Description

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 dimegabytes. 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”.

题目大意:有一个软件,要从1升级到n。每个升级有一个花费,用了P之后就不能再用L,求最小花费。

思路:正解是DP?不管。我们用最短路。建双层图,对于a→b L,在第一层建一条边。对于a→b P,从第一层的a建一条边到第二层的b,再从第二层的a建一条边到第二层的b。对于a→b C,第一层建一条边,第二层建一条边。再从第一层的n建一条边到第二层的n,费用为0。那么就保证了走过了P之后不会再走L,用SPFA求个最短路圆满解决。个人认为比D好写多了。我们要把图论发扬光大O(∩_∩)O~

代码(31MS):

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long LL; const int MAXN = ;
const int MAXE = MAXN * ; int head[MAXN];
int to[MAXE], next[MAXE], cost[MAXE];
int n, m, st, ed, ecnt; void init() {
memset(head, , sizeof(head));
ecnt = ;
} void add_edge(int u, int v, int c) {
to[ecnt] = v; cost[ecnt] = c; next[ecnt] = head[u]; head[u] = ecnt++;
//printf("%d->%d %d\n", u, v, c);
} char s[]; void input() {
scanf("%d%d", &n, &m);
int a, b, c;
for(int i = ; i < m; ++i) {
scanf("%d%d%d%s", &a, &b, &c, s);
if(*s == 'P') {
add_edge(a, b + n, c);
add_edge(a + n, b + n, c);
}
if(*s == 'L') {
add_edge(a, b, c);
}
if(*s == 'C') {
add_edge(a, b, c);
add_edge(a + n, b + n, c);
}
}
add_edge(n, n + n, );
st = , ed = * n;
} LL dis[MAXN];
bool vis[MAXN]; void SPFA() {
memset(dis, , sizeof(dis));
memset(vis, , sizeof(vis));
queue<int> que; que.push(st);
dis[st] = ;
while(!que.empty()) {
int u = que.front(); que.pop();
vis[u] = false;
for(int p = head[u]; p; p = next[p]) {
int &v = to[p];
if(dis[v] == - || dis[v] > dis[u] + cost[p]) {
dis[v] = dis[u] + cost[p];
if(!vis[v]) que.push(v);
vis[v] = true;
}
}
}
} void output() {
if(dis[ed] == -) puts("Offline");
else {
puts("Online");
cout<<dis[ed]<<endl;
}
} int main() {
init();
input();
SPFA();
output();
}

URAL 1741 Communication Fiend(最短路径)的更多相关文章

  1. DP/最短路 URAL 1741 Communication Fiend

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

  2. Ural 1741 Communication Fiend(隐式图+虚拟节点最短路)

    1741. Communication Fiend Time limit: 1.0 second Memory limit: 64 MB Kolya has returned from a summe ...

  3. URAL 1741 Communication Fiend

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

  4. 1741. Communication Fiend(dp)

    刷个简单的DP缓缓心情 1A #include <iostream> #include<cstdio> #include<cstring> #include< ...

  5. URAL DP第一发

    列表: URAL 1225 Flags URAL 1009 K-based Numbers URAL 1119 Metro URAL 1146 Maximum Sum URAL 1203 Scient ...

  6. URAL 1297 Palindrome 后缀数组

    D - Palindrome Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Subm ...

  7. URAL 1297 最长回文子串(后缀数组)

    1297. Palindrome Time limit: 1.0 secondMemory limit: 64 MB The “U.S. Robots” HQ has just received a ...

  8. POJ 1502 MPI Maelstrom / UVA 432 MPI Maelstrom / SCU 1068 MPI Maelstrom / UVALive 5398 MPI Maelstrom /ZOJ 1291 MPI Maelstrom (最短路径)

    POJ 1502 MPI Maelstrom / UVA 432 MPI Maelstrom / SCU 1068 MPI Maelstrom / UVALive 5398 MPI Maelstrom ...

  9. Johnson 全源最短路径算法

    解决单源最短路径问题(Single Source Shortest Paths Problem)的算法包括: Dijkstra 单源最短路径算法:时间复杂度为 O(E + VlogV),要求权值非负: ...

随机推荐

  1. React Native 中组件的生命周期(转)

    概述 就像 Android 开发中的 View 一样,React Native(RN) 中的组件也有生命周期(Lifecycle).所谓生命周期,就是一个对象从开始生成到最后消亡所经历的状态,理解生命 ...

  2. 【模板】素数测试(Miller-Rabin测试)

    基础素数测试模板 对于大数的素性判断,目前Miller-Rabin算法应用最广泛.一般底数仍然是随机选取,但当待测数不太大时,选择测试底数就有一些技巧了.比如,如果 被测数小于4759123141,那 ...

  3. oracle-sql优化-通过分组和缓存减少不必要的读

    环境:aix 7.1,oracle12.1.0.2 cdb 优化前SQL select * from (select row_.*, rownum rownum_ from (select '弱覆盖' ...

  4. 禁止鼠标点右键 - 防止刷新页面 - 禁止复制 chrome 和 firefox不能复制

    document.oncontextmenu = function () {//点右键,啥反应都没有了 return false; } document.onkeydown = function () ...

  5. Vue项目中使用vw实现移动端适配

    我们在vue移动端项目中的适配一般都采用rem,但是rem也不是能兼容所有的终端. 随着viewport单位越来越受到众多浏览器的支持,下面将简单介绍怎么实现vw的兼容问题,用vw代替rem 当我们采 ...

  6. 终于搞定了cxgrid的多行表头(转终于搞定了cxgrid的多行表头 )

    终于搞定了cxgrid的多行表头 转自:http://mycreature.blog.163.com/blog/static/556317200772524226400/     这一周都在处理dbg ...

  7. Teen Readers【青少年读者】

    Teen Readers Teens and younger children are reading a lot less for fun, according to a Common Sense ...

  8. 最短路径算法 4.SPFA算法(1)

    今天所说的就是常用的解决最短路径问题最后一个算法,这个算法同样是求连通图中单源点到其他结点的最短路径,功能和Bellman-Ford算法大致相同,可以求有负权的边的图,但不能出现负回路.但是SPFA算 ...

  9. 为什么我要放弃javaScript数据结构与算法(第三章)—— 栈

    有两种结构类似于数组,但在添加和删除元素时更加可控,它们就是栈和队列. 第三章 栈 栈数据结构 栈是一种遵循后进先出(LIFO)原则的有序集合.新添加的或待删除的元素都保存在栈的同一端,称为栈顶,另一 ...

  10. shell重温---基础篇(函数操作)

        linux shell 可以用户定义函数,然后在shell脚本中可以随便调用.shell中函数的定义格式如下: [ function ] funname [()] { action; [ret ...