1741. Communication Fiend

Time limit: 1.0 second
Memory limit: 64 MB

Kolya has returned from a summer camp and now he's a real communication fiend. He spends all his free time on the Web chatting with his friends via ICQ. However, lately the protocol of this service was changed once again, and Kolya's client stopped working. Now, in order to communicate with his friends again, Kolya has to upgrade his client from version 1 to version n.

Kolya has found m upgrade programs on the Web. The i-th program upgrades the client from version xi to version yi and its size is di megabytes. Each program can be installed on the corresponding version of the client only; it can't be installed on either earlier or later versions.

The first version, which is currently installed on Kolya's computer, is licensed, and many of the upgrade programs are pirate copies. If a pirate upgrade program is used, the client will always be pirated after that, whatever upgrade is used later. Some of the licensed upgrade programs can be installed on a pirate version of the client, and some of them can't. All the pirate upgrade programs can be installed on both licensed and pirate versions of the client.

Kolya is missing his friends very much, so he wants to download the necessary upgrade programs as soon as possible. Unfortunately, his Web connection is not very fast. Help Kolya determine the minimal total traffic volume necessary for upgrading the client from version 1 to version n. Kolya doesn't care if the final version n of his client is licensed or not.

Input

The first line contains space-separated integers n and m (2 ≤ n ≤ 104; 1 ≤ m ≤ 104).

Each of the following m lines describes one upgrade program in the form “xi yi di si”. Here, si is the type of the program: “Pirated”, “Cracked”, or “Licensed”. A cracked upgrade program is a licensed program that can be installed on a pirate version of the client, and a licensed program can't be installed on a pirate version. The numbers xi and yi mean that the program is installed on version xi of the client and upgrades it to version yi. The number di is the size of the program in megabytes (1 ≤ xi < yi ≤ n; 1 ≤ di ≤ 106). The data in each line are separated with exactly one space.

Output

If Kolya can upgrade the client from version 1 to version n, output “Online” in the first line and the minimal necessary total incoming traffic volume in the second line.

If it is impossible to upgrade the client, output “Offline”.

Samples

input output
  1. 3 4
  2. 1 3 10 Licensed
  3. 1 2 2 Pirated
  4. 2 3 3 Licensed
  5. 2 3 6 Cracked
  1. Online
  2. 8
  1. 3 1
  2. 1 2 10 Licensed
  1. Offline
Problem Author: Alex Samsonov (prepared by Marina Mukhacheva)
Problem Source: XIV Open USU Championship

题目链接:Ural 1741

题目虽说正规解法是DP,但是似乎是可以用最短路来做的,一开始想用d[i][k]表示到达i时系统状态是k然后建图进行spfa,然而最好还是WA10。

题目中间的版本变化似乎没有讲清楚,就是说用了某个升级包之后会使得当前系统的正版状态发生变化,尤其是Cracked包,用之后的状态跟用之前的状态是保持一致的,虽然题目中说它也是一个Licensed,但是不能使得盗版变成正版。

然后想了另外一种思路才A掉

可以将正版的系统节点看成1~n,盗版就是n+1~n+n,然后显然有:对于Lisenced版本只能从正版升级到正版,只能添加一条u->v的单向边;对于Pirate版本可以从正版升级到盗版也可以从盗版继续升级到盗版,因此添加u->v+n,与u+n->v+n;对于Cracked版本就是看成一种桥,盗版可以继续盗版,正版继续正版,

即u->v与u+n->v+n,然后跑一个spfa,看d[n]正版和d[n<<1]盗版的值就行了

提供一组数据用来说明Cracked的作用

4 3
1 2 1 Pirated
2 3 3 Cracked
3 4 6 Licensed

答案应为 Offline

代码:

  1. #include<stdio.h>
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4. #define INF 0x3f3f3f3f
  5. #define CLR(x,y) memset(x,y,sizeof(x))
  6. #define LC(x) (x<<1)
  7. #define RC(x) ((x<<1)+1)
  8. #define MID(x,y) ((x+y)>>1)
  9. typedef pair<long long,int> pli;
  10. typedef long long LL;
  11. const double PI=acos(-1.0);
  12.  
  13. const int N=2e4+10;
  14. struct edge
  15. {
  16. int to;
  17. int pre;
  18. LL w;
  19. };
  20. edge E[N];
  21. int head[N],tot;
  22. LL d[N];
  23.  
  24. void add(int s,int t,LL w)
  25. {
  26. E[tot].to=t;
  27. E[tot].w=w;
  28. E[tot].pre=head[s];
  29. head[s]=tot++;
  30. }
  31. void init()
  32. {
  33. CLR(d,INF);
  34. CLR(head,-1);
  35. tot=0;
  36. }
  37. void spfa(int s)
  38. {
  39. priority_queue<pli>Q;
  40. d[s]=0LL;
  41. Q.push(pli(-d[s],s));
  42. while (!Q.empty())
  43. {
  44. int now=Q.top().second;
  45. Q.pop();
  46. for (int i=head[now]; ~i; i=E[i].pre)
  47. {
  48. int v=E[i].to;
  49. LL w=E[i].w;
  50. if(d[v]>d[now]+w)
  51. {
  52. d[v]=d[now]+w;
  53. Q.push(pli(-d[v],v));
  54. }
  55. }
  56. }
  57. }
  58.  
  59. int main(void)
  60. {
  61. int n,m,i,u,v;
  62. LL D;
  63. char flag[15];
  64. while (cin>>n>>m)
  65. {
  66. init();
  67. LL inf=d[0];
  68. for (i=0; i<m; ++i)
  69. {
  70. cin>>u>>v>>D>>flag;
  71. if(flag[0]=='C')
  72. {
  73. add(u,v,D);
  74. add(n+u,n+v,D);
  75. }
  76. else if(flag[0]=='L')
  77. {
  78. add(u,v,D);
  79. }
  80. else if(flag[0]=='P')
  81. {
  82. add(u,n+v,D);
  83. add(n+u,n+v,D);
  84. }
  85. }
  86. spfa(1);
  87. LL ans=min<LL>(d[n],d[n<<1]);
  88. if(ans==inf)
  89. cout<<"Offline"<<endl;
  90. else
  91. cout<<"Online"<<endl<<ans<<endl;
  92. }
  93. return 0;
  94. }

Ural 1741 Communication Fiend(隐式图+虚拟节点最短路)的更多相关文章

  1. DP/最短路 URAL 1741 Communication Fiend

    题目传送门 /* 题意:程序从1到n版本升级,正版+正版->正版,正版+盗版->盗版,盗版+盗版->盗版 正版+破解版->正版,盗版+破解版->盗版 DP:每种情况考虑一 ...

  2. URAL 1741 Communication Fiend(最短路径)

    Description Kolya has returned from a summer camp and now he's a real communication fiend. He spends ...

  3. URAL 1741 Communication Fiend

    URAL 1741 思路: dp 状态:dp[i][1]表示到第i个版本为正版的最少流量花费 dp[i][0]表示到第i个版本为盗版的最少流量花费 初始状态:dp[1][0]=dp[0][0]=0 目 ...

  4. 【UVA】658 - It&#39;s not a Bug, it&#39;s a Feature!(隐式图 + 位运算)

    这题直接隐式图 + 位运算暴力搜出来的,2.5s险过,不是正法,做完这题做的最大收获就是学会了一些位运算的处理方式. 1.将s中二进制第k位变成0的处理方式: s = s & (~(1 < ...

  5. 八数码问题+路径寻找问题+bfs(隐式图的判重操作)

    Δ路径寻找问题可以归结为隐式图的遍历,它的任务是找到一条凑够初始状态到终止问题的最优路径, 而不是像回溯法那样找到一个符合某些要求的解. 八数码问题就是路径查找问题背景下的经典训练题目. 程序框架 p ...

  6. uva658(最短路径+隐式图+状态压缩)

    题目连接(vj):https://vjudge.net/problem/UVA-658 题意:补丁在修正 bug 时,有时也会引入新的 bug.假定有 n(n≤20)个潜在 bug 和 m(m≤100 ...

  7. nyoj 21--三个水杯(隐式图bfs)

    三个水杯 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 给出三个水杯,大小不一,并且只有最大的水杯的水是装满的,其余两个为空杯子.三个水杯之间相互倒水,并且水杯没有标识 ...

  8. UVA 658 状态压缩+隐式图+优先队列dijstla

    不可多得的好题目啊,我看了别人题解才做出来的,这种题目一看就会做的实在是大神啊,而且我看别人博客都看了好久才明白...还是对状态压缩不是很熟练,理解几个位运算用了好久时间.有些题目自己看着别人的题解做 ...

  9. UVA - 658 It's not a Bug, it's a Feature! (隐式图的最短路,位运算)

    隐式的图搜索,存不下边,所以只有枚举转移就行了,因为bug的存在状态可以用二进制表示,转移的时候判断合法可以用位运算优化, 二进制pre[i][0]表示可以出现的bug,那么u&pre[i][ ...

随机推荐

  1. MFC中挂起线程和恢复线程

    DWORD SuspendThread ( HANDLE hThread );   //挂起线程DWORD ResumeThread ( HANDLE hThread );   //恢复线程 比如说我 ...

  2. ubuntu 图形界面查看隐藏文件

    在 Linux 下以 . 开头的文件或文件夹为隐藏文件,在图形界面(nautilus)下可用 CTRL + H 显示隐藏文件,终端下者可以用 ls -a 显示所有文件.

  3. Mac相关命令

    1,查询端口占用与Kill相应进程 lsof -i:端口,查询端口的占用情况 kill PID,关闭指定PID的进程. 如: localhost:~ tianjingcheng$ kill 729 l ...

  4. 菜鸟学Linux命令:grep配合ls等使用

    linux grep命令 (global search regular expression(RE) and print out the line )是一种强大的文本搜索工具,它能使用正则表达式搜索文 ...

  5. 批量传递ID数组字符串到后台的处理

    js代码: $(function () { $("#btnTest").click(function () { var array = new Array(); array.pus ...

  6. WPF中的VisualTreeHelper

    VisualTreeHelper提供了一组WPF控件树模型,通过VisualTreeHelper可以遍历控件的树形结构,获取我们所需要的控件以及相应的属性: VisualTreeHelper提供了一下 ...

  7. x264码率控制方法介绍

    转自:http://www.bubuko.com/infodetail-471698.html 1.  X264显式支持的一趟码率控制方法有:ABR, CQP, CRF. 缺省方法是CRF.这三种方式 ...

  8. diff和common

    diff 命令 diff命令:找出两个文件的不同点,用于比较文件的差异 linux上非常重要的工具,一般用于制作补丁文件,特别是比较两个版本不同的文件以找到改动的地方. diff在命令行中打印每一个行 ...

  9. [转载]有了 malloc/free 为什么还要 new/delete ?

      malloc 与free 是C++/C 语言的标准库函数,new/delete 是C++的运算符.他们都可以用于申请动态内存和释放内存.      对于非内部数据类型的对象(如类对象)而言,光用m ...

  10. RabbitMQ原理

    vhosts(broker) connection 与 channel(连接与信道) exchange 与  routingkey(交换机与路由键) queue(队列) Binding(绑定) cli ...