POJ 1797(SPFA变种)-spfa能做很多!

题目大意:

求1到n的可行路径上最小值的最大值,也就是说从1到n的每一条可行路径上都有一条最小值,有多条路径的话就求这些最小值的最大值。

思路:

spfa的变种,将spfa中的dis[i]改为me[i], 代表从1到i的路径中最小边的最大值,提示到这里了,怎么更新就不在罗嗦了,有疑问就看代码吧!

 

View Code
 1 #include <cstdio>

 2 #include <cstdlib>

 3 #include <cstring>

 4 #include <iostream>

 5 #define N 1010

 6 #define M 1001000

 7 using namespace std;

 8 int n,m,to[M],next[M],head[N],len[M],me[N],q[M<<4],cnt,tt;//me 代表从1到i的路径中最小边的最大值 

 9 bool vis[N];

10 inline void add(int u,int v,int w)

11 {

12     to[cnt]=v; len[cnt]=w; next[cnt]=head[u]; head[u]=cnt++;

13 }

14 void read()

15 {

16     memset(head,-1,sizeof head);

17     memset(me,0,sizeof me);

18     cnt=1;

19     scanf("%d%d",&n,&m);

20     for(int i=1,a,b,c;i<=m;i++)

21     {

22         scanf("%d%d%d",&a,&b,&c);

23         add(a,b,c); add(b,a,c);

24     }

25 }

26 void spfa(int st)

27 {

28     int h=1,t=2,sta,sy;

29     q[1]=st; vis[st]=1; me[st]=0x7f7f7f7f;

30     while(h<t)

31     {

32         sta=q[h++];

33         vis[sta]=0;

34         for(int i=head[sta];~i;i=next[i])

35         {

36             sy=min(len[i],me[sta]);

37             if(me[to[i]]<sy)

38             {

39                 me[to[i]]=sy;

40                 if(!vis[to[i]])

41                 {

42                     vis[to[i]]=1;

43                     q[t++]=to[i];

44                 }

45             }

46         }

47     }

48 }

49 void go()

50 {

51     spfa(1);

52     printf("%d\n\n",me[n]);

53 }

54 int main()

55 {

56     scanf("%d",&tt);

57     for(int i=1;i<=tt;i++)

58     {

59         printf("Scenario #%d:\n",i);

60         read();

61         go();

62     }

63     system("pause");

64     return 0;

65 } 

你可能感兴趣的:(SPFA)