CODEVS——T1961 躲避大龙

 时间限制: 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。

分类标签 Tags 点此展开 

 
 
用vis[i][j]表示i点在j秒可以访问这个点
搜一下就可以了~~
 1 #include <algorithm>
 2 #include <cstdio>
 3 #include <queue>
 4 
 5 using namespace std;
 6 
 7 const int M(9000+15);
 8 const int N(7000+15);
 9 
10 int n,m,u,v,t;
11 
12 int head[N],sumedge;
13 struct Edge
14 {
15     int u,v,tim,next;
16     Edge(int u=0,int v=0,int next=0,int tim=0):
17         u(u),v(v),next(next),tim(tim) {}
18 }edge[M<<1];
19 void ins(int u,int v,int tim)
20 {
21     edge[++sumedge]=Edge(u,v,head[u],tim);
22     head[u]=sumedge;
23 }
24 
25 struct TypeNodePostion
26 {
27     int pos,tim;
28 }now,to,pre;
29 queue<TypeNodePostion>que;
30 int vis[N][61];
31 void SPFA()
32 {
33     now.pos=1;now.tim=0;
34     que.push(now);vis[1][0]=1;
35     while(!que.empty())
36     {
37         pre=que.front();que.pop();
38         for(int i=head[pre.pos];i;i=edge[i].next)
39         {
40             to.pos=edge[i].v;
41             to.tim=((pre.tim+edge[i].tim%60)+60)%60;
42             if(!vis[to.pos][to.tim])
43             {
44                 que.push(to);
45                 vis[to.pos][to.tim]=1;
46             }
47         }
48     }
49 }
50 
51 int main()
52 {
53     scanf("%d%d",&n,&m);
54     for(;m;m--)
55     {
56         scanf("%d%d%d",&u,&v,&t);
57         ins(u,v,t); ins(v,u,t);
58     }
59     SPFA();
60     int ans=60;
61     for(int i=0;i<60;i++)
62         if(vis[2][i])
63         {
64             ans=i;
65             break;
66         }
67     if(ans<10) putchar('0');
68     printf("%d",ans);
69     return 0;
70 }
——每当你想要放弃的时候,就想想是为了什么才一路坚持到现在。
原文地址:https://www.cnblogs.com/Shy-key/p/6971963.html