codevs1961 躲避大龙
来源:互联网 发布:淘宝客服服务回复 编辑:程序博客网 时间:2024/05/03 06:38
题目描述 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。
对于这个题目,我们可以开一个二维数组used[i][k]表示在第k秒钟(%60)是否到达过i号节点,最后我们可以从小到大找一遍,只要找到最小的那个值就是ans
代码如下:
#include<iostream>#include<cstdio>#include<cstdlib>#include<queue>using namespace std;const int maxn=50005;struct dqs{ int f,t,c;}hh[maxn];struct dqm{ int pos,tim;};int tot=0,first[maxn],next[maxn];void build(int f,int t,int c){ hh[++tot]=(dqs){f,t,c}; next[tot]=first[f]; first[f]=tot;}bool used[maxn][60];queue<dqm>q;void spfa(){ used[1][0]=1; dqm begin; begin.pos=1; begin.tim=0; q.push(begin); while(!q.empty()) { dqm x=q.front(); q.pop(); dqm now; for(int i=first[x.pos];i;i=next[i]) { now.pos=hh[i].t; now.tim=(x.tim+hh[i].c)%60; while(now.tim<0) now.tim+=60; if(!used[now.pos][now.tim]) { used[now.pos][now.tim]=1; q.push(now); } } }}int main(){ int n,m; scanf("%d%d",&n,&m); for(int i=1;i<=m;i++) { int a,b,c; scanf("%d%d%d",&a,&b,&c); build(a,b,c); build(b,a,c); } spfa(); int ans; for(ans=0;ans<=59;ans++) if(used[2][ans]) break; if(ans<10) printf("0"); printf("%d",ans); return 0;}
- Codevs1961 躲避大龙
- codevs1961 躲避大龙
- [codevs1961]躲避大龙
- codevs1961 躲避大龙(spfa)
- CodeVS1961躲避大龙 题解【图论】【SPFA】【搜索】
- 【codevs 1961】躲避大龙
- 【codevs 1961】 躲避大龙
- Codevs 1961 躲避大龙
- codevs 1961 躲避大龙
- 【codevs 1961】躲避大龙
- Codevs 1961 躲避大龙 spfa
- codevs 1961 躲避大龙(spfa)
- codevs 1961 躲避大龙 题解报告
- codevs 1961 躲避大龙 bfs
- CODEVS 1961 躲避大龙(CODEVS200题纪念)
- codevs 1961 躲避大龙 解题报告 SPFA
- 躲避全面预算管理软件选型两大误区
- 争吵、躲避
- spring下应用@Resource, @Autowired 和 @Inject注解进行依赖注入的差异
- 1003. Prime Palindromes
- 观察者模式——————个人理解
- MySQL数据库删除后的恢复工作
- IO之随机读写流RandomAccessFile
- codevs1961 躲避大龙
- IO(四)
- Java操作properties文件
- Windows通过系统命令sc,添加进系统服务中
- SCOI2011 糖果
- Docker容器内多进程管理(一)-Supervisor
- POJ1011 搜索+剪枝【很好】
- 2016.10.30 Bootstrap.5(组件)
- 左旋字符串k个字符