【codevs 1961】 躲避大龙
2016-10-28 14:56
274 查看
1961 躲避大龙
时间限制: 1 s
空间限制: 128000 KB
题目等级 : 钻石 Diamond
题解
查看运行结果
题目描述 Description
你早上起来,慢悠悠地来到学校门口,发现已经是八点整了!(这句话里有一个比较重要的条件)
学校共有N个地点,编号为1~N,其中1号为学校门口(也就是你现在所处的位置),2号为你的教室(也就是你的目的地)。这些地点之间有M条双向道路,对于第i条道路,为了不引起值周队老师的怀疑,你通过它的时间须恰好为Ti秒。这个数可能为负数,意义为时间倒流。
不过,即使没有引起怀疑,值周队也布下了最后一道防线:大龙会在教室处不定期出现。当然,你也了解大龙的习性:当前时间的秒数越小,大龙出现的概率就越低,例如:8:13:06这一时刻的秒数是06,就要比8:12:57这个时刻更加安全。
现在的问题是,在不引起怀疑的前提下,最安全的到达时刻的秒数是多少。如果学校门口到教室没有路(-_-||),请输出60。
注意,你可以选择在途中的任何时候经过教室,而不结束“旅程”,具体见样例。
输入描述 Input Description
第一行为两个整数,N和M,意义在上面已经说过了。
第2行~第M+1行,每行代表一条道路。第i+1行代表第i条道路,这一行有3个整数,Ai,Bi,Ti,表示Ai号地点与Bi号地点有一条双向道路,通过它的时间必须为Ti秒。
输出描述 Output Description
只有一行,为最安全的到达时刻的秒数。
样例输入 Sample Input
Input1:
2 1
2 1 54
Input2:
3 3
1 2 26
1 3 17
2 3 -9
Input3:
3 1
1 3 110
Input4:
2 2
1 2 7
2 1 9
Input5:
2 2
1 2 3
1 1 1
Input6:
2 2
1 2 9
1 2 11
样例输出 Sample Output
Output1:
06
Output2:
00
Output3:
60
Output4:
01
Output5:
00
Output6:
01
数据范围及提示 Data Size & Hint
样例1的说明:一共只有两个地点(多么福利的数据啊),也只有一条道路,耗时为54秒。最优方案为,经过这个道路9次,耗时486秒,即8分06秒,于8:08:06到达教室。当然,最优方案不唯一。
样例2的说明:走1->3->1->2,用时17+17+26,于8:01:00到达;或走1->2->3->1->2,用时26-9+17+26,于8:01:00到达。
对于20%的数据,N≤2;
对于40%的数据,N≤100;
对于70%的数据,N≤1000;
对于100%的数据,2≤N≤7000,0≤M≤9000,1≤Ai,Bi≤N,|Ti|≤109
QAQ这个题其实好多好多点啊
时间限制: 1 s
空间限制: 128000 KB
题目等级 : 钻石 Diamond
题解
查看运行结果
题目描述 Description
你早上起来,慢悠悠地来到学校门口,发现已经是八点整了!(这句话里有一个比较重要的条件)
学校共有N个地点,编号为1~N,其中1号为学校门口(也就是你现在所处的位置),2号为你的教室(也就是你的目的地)。这些地点之间有M条双向道路,对于第i条道路,为了不引起值周队老师的怀疑,你通过它的时间须恰好为Ti秒。这个数可能为负数,意义为时间倒流。
不过,即使没有引起怀疑,值周队也布下了最后一道防线:大龙会在教室处不定期出现。当然,你也了解大龙的习性:当前时间的秒数越小,大龙出现的概率就越低,例如:8:13:06这一时刻的秒数是06,就要比8:12:57这个时刻更加安全。
现在的问题是,在不引起怀疑的前提下,最安全的到达时刻的秒数是多少。如果学校门口到教室没有路(-_-||),请输出60。
注意,你可以选择在途中的任何时候经过教室,而不结束“旅程”,具体见样例。
输入描述 Input Description
第一行为两个整数,N和M,意义在上面已经说过了。
第2行~第M+1行,每行代表一条道路。第i+1行代表第i条道路,这一行有3个整数,Ai,Bi,Ti,表示Ai号地点与Bi号地点有一条双向道路,通过它的时间必须为Ti秒。
输出描述 Output Description
只有一行,为最安全的到达时刻的秒数。
样例输入 Sample Input
Input1:
2 1
2 1 54
Input2:
3 3
1 2 26
1 3 17
2 3 -9
Input3:
3 1
1 3 110
Input4:
2 2
1 2 7
2 1 9
Input5:
2 2
1 2 3
1 1 1
Input6:
2 2
1 2 9
1 2 11
样例输出 Sample Output
Output1:
06
Output2:
00
Output3:
60
Output4:
01
Output5:
00
Output6:
01
数据范围及提示 Data Size & Hint
样例1的说明:一共只有两个地点(多么福利的数据啊),也只有一条道路,耗时为54秒。最优方案为,经过这个道路9次,耗时486秒,即8分06秒,于8:08:06到达教室。当然,最优方案不唯一。
样例2的说明:走1->3->1->2,用时17+17+26,于8:01:00到达;或走1->2->3->1->2,用时26-9+17+26,于8:01:00到达。
对于20%的数据,N≤2;
对于40%的数据,N≤100;
对于70%的数据,N≤1000;
对于100%的数据,2≤N≤7000,0≤M≤9000,1≤Ai,Bi≤N,|Ti|≤109
QAQ这个题其实好多好多点啊
#include <iostream> #include <cstdio> #include <queue> #include <cstring> #include <algorithm> using namespace std; const int MAXN = 200005; int n,m,f,t,v; int s,e,ans = -1,tot = 0; int first[MAXN],next[MAXN]; bool use[MAXN][65];//第i个点在秒数为j的时候能不能到达 bool xc = true,flag = false; struct edge { int f,t,v; }l[MAXN]; struct zt { int x,t; }; void build(int f,int t,int v) { l[++ tot] = (edge){f,t,v}; next[tot] = first[f]; first[f] = tot; return; } queue < zt > q; void spfa(int s) { memset(use,0,sizeof(use)); while(!q.empty()) q.pop(); q.push((zt){s,0}); while(!q.empty()) { zt u = q.front(); q.pop(); for(int i = first[u.x]; i != -1; i = next[i]) { int w = l[i].t; int t = (u.t + l[i].v) % 60;//QAQ不先%会TLE!! while(t < 0) t += 60; if(!use[w][t]) q.push((zt){w,t}); use[w][t] = true; } } return; } int main() { memset(first,0xff,sizeof(first)); scanf("%d %d",&n,&m); for(int i = 1; i <= m; i ++) { scanf("%d %d %d",&f,&t,&v); build(f,t,v),build(t,f,v); } spfa(1); /* for(ans = 0; ans < 60; ans ++) if(use[2][ans]) break;*/ //这两个最大的区别就是i最后是60还是61! //ans初始化成0的话就没办法判断啦 for(int i = 0; i <= 60; i ++) if(use[2][i]) { ans = i; break; } if(ans == -1)//走不到~~ { puts("60"); return 0; } if(ans < 10) printf("0%d\n",ans); else printf("%d\n",ans); return 0; }
相关文章推荐
- [codevs 1961]躲避大龙(dfs)
- 【codevs 1961】躲避大龙
- Codevs 1961 躲避大龙
- codevs 1961 躲避大龙
- 【codevs 1961】躲避大龙
- codevs 1961 躲避大龙 bfs
- Codevs 1961 躲避大龙 spfa
- 躲避大龙(codevs 1961)
- Codevs1961 躲避大龙
- codevs 1961 躲避大龙 题解报告
- codevs1961 躲避大龙
- [codevs1961]躲避大龙
- CodeVS1961躲避大龙 题解【图论】【SPFA】【搜索】
- 1961 躲避大龙(dfs)
- 1961 躲避大龙
- codevs 1961 躲避大龙 解题报告 SPFA
- Codevs 1961 躲避大龙
- [Codevs] 1961 躲避大龙
- CODEVS 1961 躲避大龙(CODEVS200题纪念)
- codevs 1961 躲避大龙(spfa)