最短路

网站:CSUST 8月12日(最短路)

A,C,D,E都是Floyd算法........代码就不发了。

B   最短路径问题   HDU 3790

代码:    328MS

 1 #include<cstdio>

 2 #include<cstring>

 3 #include<iostream>

 4 using namespace std;

 5 const int MAX = 1000+5,inf = 1061109568;

 6 int map[MAX][MAX][2],visit[MAX],T[MAX],cost[MAX],n,m;

 7 int main() 

 8 {

 9     int a,b,d,p,s,e;

10     while(scanf("%d%d",&n,&m),n+m)

11      {

12         memset(map,0x3f,sizeof(map));

13         memset(cost,0x3f,sizeof(cost));

14         memset(T,0x3f,sizeof(T)); 

15         for(int i = 1;i<=m;++i)

16          {

17             scanf("%d%d%d%d",&a,&b,&d,&p);

18             if(d<map[a][b][0]) 

19             {      //对于输入的处理很重要 

20                 map[a][b][0] = map[b][a][0] = d;

21                 map[a][b][1] = map[b][a][1] = p;

22             }

23             else if(d==map[a][b][0])

24             map[a][b][1] = map[b][a][1]=min(map[a][b][1],p);

25         }

26         scanf("%d%d",&s,&e);

27         T[s] = cost[s] = 0;   //将起点的花费和路径长度都赋0 

28         T[0] = inf-1;       //为便于更新s的值,将T[0]赋无穷大 

29         memset(visit,0,sizeof(visit));

30         while(1)

31          {

32             for(int i = 1;i<=n;++i)

33             if(!visit[i]&&(T[s]+map[s][i][0])<=T[i]) 

34             {

35                 if(T[s]+map[s][i][0]<T[i]) 

36                 { //这一步的处理很重要,要将等于和小于的情况分开处理 

37                     T[i] = T[s] + map[s][i][0];

38                     cost[i] = cost[s] + map[s][i][1];

39                 }

40                 else if(T[s]+map[s][i][0]==T[i])

41                 cost[i] = min(cost[i],cost[s] + map[s][i][1]);

42             }

43             visit[s] = 1;

44             s = 0; //将s做初始化处理 

45             bool flag = 1;

46             for(int i = 1;i<=n;++i)

47             if(!visit[i]&&T[i]<T[s]) 

48             {

49                 flag = 0;

50                 s = i;

51             }

52             if(flag) //设置退出条件 

53             break;

54         }

55         printf("%d %d\n",T[e],cost[e]);

56     }

57     return 0;

58 }

 H   凶残的看不懂题的题......还是Floyd    Tram     POJ 1847     16ms

 1 #include<cstdio> 

 2 #include<cstring>

 3 #include<algorithm>

 4 #include<iostream>

 5 using namespace std;

 6 const int maxn = 110;

 7 const int INF = 10000000;

 8 int dist[maxn][maxn];

 9 int n,start, end;

10 int main()

11 {

12     while(~scanf("%d%d%d",&n,&start,&end))

13     {

14         for(int i=1;i<=n;i++)

15         {

16             for(int j=1;j<=n;j++)

17                 if(i==j)

18                     dist[i][j]=0;

19                 else

20                     dist[i][j] = INF;

21         }

22         int path,y;

23         for(int i=1;i<=n;i++)

24         {

25             scanf("%d",&path);

26             for(int j=1;j<=path;j++)

27             {

28                 scanf("%d",&y);

29                 if(j==1)

30                     dist[i][y]=0;

31                 else

32                     dist[i][y]=1;

33             }

34         }

35         for(int i=1;i<=n;i++)

36             for(int j=1;j<=n;j++)

37                 for(int k=1;k<=n;k++)

38                     dist[j][k] =min(dist[j][k],dist[j][i] + dist[i][k] );

39     }

40     if(dist[start][end]==INF)

41         printf("-1\n");

42     else

43         printf("%d\n",dist[start][end]);

44     return 0;

45 }

 I     昂贵的聘礼    POJ 1062

代码:      0ms

 1 #include <stdio.h>

 2 #include <string.h>

 3 const int INF = 0x3f3f3f3f;

 4 int map[110][110];

 5 int rank[110],dis[110],vis[110];

 6 int n,m;

 7 int Dijkstra(int k)

 8 {

 9     int i,j;

10     memset(vis,0,sizeof(vis));

11     vis[0] = 1;

12     for(i = 0; i <= n; i++)

13         dis[i] = map[0][i];

14     dis[0] = 0;

15     for(i = 1; i <= n; i++)

16     {

17         int min = INF,pos;

18         for(j = 0; j <= n; j++)

19             if(dis[j] < min && !vis[j])

20                 min = dis[pos=j];

21         vis[pos] = 1;

22         for(j = 0; j <= n; j++)

23             if(!vis[j] && dis[j] > dis[pos] + map[pos][j] && rank[pos] >= k && rank[pos] <= k+m)

24                 dis[j] = dis[pos] + map[pos][j];

25     }

26     return dis[1];

27 }

28 int main()

29 {

30     int i;

31     int p,x,t,v;

32     memset(map, 0x3f, sizeof(map));

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

34     for(i = 1; i <= n; i++)

35     {

36         scanf("%d %d %d",&p,&rank[i],&x);

37         map[0][i] = p;

38         while(x--)

39         {

40             scanf("%d %d",&t,&v);

41             map[t][i] = v;     //能替代i的物品,编号为t,价值为v

42         }

43     }

44     int min = INF,ans;

45     for(i = rank[1]-m; i <= rank[1]; i++) //枚举从rank[1]-m到rank[1]的等级;取这些最短路中最短的;

46     {

47         ans = Dijkstra(i);

48         if(min > ans)

49             min = ans;

50     }

51     printf("%d\n",min);

52     return 0;

53 }

 

你可能感兴趣的:(最短路)