1021 玛丽卡 - Wikioi

题目描述 Description
麦克找了个新女朋友,玛丽卡对他非常恼火并伺机报复。
因为她和他们不住在同一个城市,因此她开始准备她的长途旅行。
在这个国家中每两个城市之间最多只有一条路相通,并且我们知道从一个城市到另一个城市路上所需花费的时间。
麦克在车中无意中听到有一条路正在维修,并且那儿正堵车,但没听清楚到底是哪一条路。无论哪一条路正在维修,从玛丽卡所在的城市都能到达麦克所在的城市。
玛丽卡将只从不堵车的路上通过,并且她将按最短路线行车。麦克希望知道在最糟糕的情况下玛丽卡到达他所在的城市需要多长时间,这样他就能保证他的女朋友离开该城市足够远。
编写程序,帮助麦克找出玛丽卡按最短路线通过不堵车道路到达他所在城市所需的最长时间(用分钟表示)。
输入描述 Input Description
第一行有两个用空格隔开的数N和M,分别表示城市的数量以及城市间道路的数量。1≤N≤1000,1≤M≤N*(N-1)/2。城市用数字1至N标识,麦克在城市1中,玛丽卡在城市N中。
接下来的M行中每行包含三个用空格隔开的数A,B和V。其中1≤A,B≤N,1≤V≤1000。这些数字表示在A和城市B中间有一条双行道,并且在V分钟内是就能通过。

输出描述 Output Description
输出文件的第一行中写出用分钟表示的最长时间,在这段时间中,无论哪条路在堵车,玛丽卡应该能够到达麦克处,如果少于这个时间的话,则必定存在一条路,该条路一旦堵车,玛丽卡就不能够赶到麦克处。
样例输入 Sample Input
5 7
1 2 8
1 4 10
2 3 9
2 4 10
2 5 1
3 4 7
3 5 10
样例输出 Sample Output
27

 

 

水题,先求最短路,再删最短路上的边,再求最短路

 1 const

 2     maxn=1010;

 3     maxm=1001000;

 4 var

 5     first,dis,flag,pre:array[0..maxn]of longint;

 6     next,last,len:array[0..maxm]of longint;

 7     q:array[0..maxm*3]of longint;

 8     n,m,tot,ans,time:longint;

 9 

10 procedure insert(x,y,z:longint);

11 begin

12     inc(tot);

13     last[tot]:=y;

14     next[tot]:=first[x];

15     first[x]:=tot;

16     len[tot]:=z;

17 end;

18 

19 function spfa(x:longint):longint;

20 var

21     l,r,i:longint;

22 begin

23     l:=1;

24     r:=1;

25     q[1]:=1;

26     inc(time);

27     fillchar(dis,sizeof(dis),1);

28     dis[1]:=0;

29     flag[1]:=time;

30     while l<=r do

31       begin

32         i:=first[q[l]];

33         while i<>0 do

34           begin

35             if (i<>x) and ((i xor 1)<>x) then

36             if dis[q[l]]+len[i]<dis[last[i]] then

37             begin

38               if flag[last[i]]<>time then

39               begin

40                 inc(r);

41                 q[r]:=last[i];

42                 flag[last[i]]:=time;

43               end;

44               dis[last[i]]:=dis[q[l]]+len[i];

45               pre[last[i]]:=i;

46             end;

47             i:=next[i];

48           end;

49         flag[q[l]]:=time-1;

50         inc(l);

51       end;

52     exit(dis[n]);

53 end;

54 

55 var

56     cut:array[0..maxn]of longint;

57     num:longint;

58 

59 function max(x,y:longint):longint;

60 begin

61     if x>y then exit(x);

62     exit(y);

63 end;

64 

65 procedure main;

66 var

67     i,x,y,z:longint;

68 begin

69     read(n,m);

70     tot:=1;

71     for i:=1 to m do

72       begin

73         read(x,y,z);

74         insert(x,y,z);

75         insert(y,x,z);

76       end;

77     ans:=spfa(0);

78     i:=n;

79     while i<>1 do

80       begin

81         inc(num);

82         cut[num]:=pre[i];

83         i:=last[pre[i]xor 1];

84       end;

85     for i:=1 to num do

86       ans:=max(ans,spfa(cut[i]));

87     write(ans);

88 end;

89 

90 begin

91     main;

92 end.
View Code

 

你可能感兴趣的:(IO)