ZOJ2027

题意:一个人要去旅行 给你起点和终点 求最少花费  其中花费为经过路径的总费用减去该路径的中的最大花费 ,Floyd 变型

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <algorithm>
 6 #include <map>
 7 using namespace std;
 8 #define inf 9999999
 9 #define N 205
10 int n,m,sum[N][N],maxcost[N][N];
11 map<string,int> mp; //map映射 
12 
13 void Floyd() //Floyd变型 
14 {
15     for(int k=0; k<n; k++)
16     {
17         for(int i=0; i<n; i++)
18         {
19             if(i==k||sum[i][k]==inf) continue;
20             for(int j=0; j<n; j++)
21             {
22                 if(k==j||sum[k][j]==inf) continue;
23                 int temp = sum[i][k] + sum[k][j] - max(maxcost[i][k],maxcost[k][j]);
24                 if(temp < sum[i][j]-maxcost[i][j])//如果i经过中间点到j的总花费-最大花费小于直接到的 
25                 {
26                     sum[i][j] = sum[i][k]+sum[k][j];
27                     maxcost[i][j] = max(maxcost[i][k],maxcost[k][j]);
28                 }
29             }
30         }
31     }
32 }
33 
34 int main()
35 {
36     string st,ed;
37     while(cin>>st>>ed)
38     {
39         for(int i=0; i<N; i++)
40         for(int j=0; j<N; j++)
41         {
42             if(i==j) 
43             {
44                 sum[i][j]=0;
45                 maxcost[i][j]=0;
46             }
47             else 
48             {
49                 sum[i][j] = inf;
50                 maxcost[i][j] = -1;
51             }
52         }
53         mp.clear();
54         n = 0;
55         if(mp.find(st)==mp.end()) mp[st] = n++;
56         if(mp.find(ed)==mp.end()) mp[ed] = n++;
57         scanf("%d",&m);
58         string s1,s2;
59         int w;
60         for(int i=0; i<m; i++)
61         {
62             cin>>s1>>s2>>w;
63             if(mp.find(s1)==mp.end()) mp[s1] = n++;
64             if(mp.find(s2)==mp.end()) mp[s2] = n++;
65             sum[mp[s1]][mp[s2]] = w;
66             maxcost[mp[s1]][mp[s2]] = w;
67         }
68         Floyd();
69         int ans = sum[mp[st]][mp[ed]] - maxcost[mp[st]][mp[ed]];
70         printf("%d
",ans);
71     }
72     return 0;
73 }
View Code
原文地址:https://www.cnblogs.com/ar940507/p/3247524.html