PC/UVA 110903/10099 The Tourist Guide

题目链接:http://www.programming-challenges.com/pg.php?page=downloadproblem&probid=110903&format=html

最短路变形,也就是求最大的运载能力,Dijk变形使用,需要注意的一点是,最后求次数是要判是否能刚好运完 ,不能的话要多运一次,因为每次他都要去。

View Code
 1 #include <iostream>
2 #include <cstring>
3 #include <cmath>
4 #include <cstdio>
5 using namespace std;
6 const int INF=(1<<20);
7 int map[105][105];
8 int vist[105],dist[105];
9 int n;
10 int min(int x,int y)
11 {
12 if(x>y)return y;
13 else return x;
14 }
15 void Dijks(int s)
16 {
17 int i,j,v,ma;
18 for(i=1;i<=n;i++)
19 dist[i]=map[s][i];
20 dist[s]=0;
21 vist[s]=1;
22 for (i=1;i<n;i++)
23 {
24 ma=0;
25 for (j=1;j<=n;j++)
26 if(!vist[j]&&dist[j]>ma&&dist[j]!=INF)
27 {
28 v=j;
29 ma=dist[j];
30 }
31 vist[v]=1;
32 for (j=1;j<=n;j++)
33 {
34 if(!vist[j]&&dist[j]<min(dist[v],map[v][j])&&map[v][j]!=INF||!vist[j]&&dist[j]==INF&&map[v][j]!=INF)
35 dist[j]=min(dist[v],map[v][j]);
36 }
37 }
38 }
39 int main()
40 {
41 int m,i,j,num,s,t,p,x,y,z,k=0;
42 while (scanf("%d%d",&n,&m)&&n+m)
43 {
44 k++;
45 for (i=1;i<=n;i++)
46 {
47 for(j=1;j<=n;j++)
48 map[i][j]=INF;
49 map[i][i]=0;
50 dist[i]=INF;
51 vist[i]=0;
52 }
53 for (i=1;i<=m;i++)
54 {
55 scanf("%d%d%d",&x,&y,&z);
56 map[x][y]=map[y][x]=z;
57 }
58 scanf("%d%d%d",&s,&t,&p);
59 Dijks(s);
60 num=0;
61 if((p/dist[t]+1+p)%dist[t])num+=1;//不能正好运完,要多跑一次
62 num+=(p/dist[t]+1+p)/dist[t]; //正好运完
63
64
65 printf("Scenario #%d\n",k);
66 printf("Minimum Number of Trips = %d\n\n",num);
67 }
68 return 0;
69
70 }



posted @ 2012-03-13 21:16  我们一直在努力  阅读(270)  评论(0编辑  收藏  举报