ZOJ Problem Set - 2027
来源:互联网 发布:武汉富士康2018java 编辑:程序博客网 时间:2024/06/09 03:52
Samball is going to travel in the coming vacation. Now it's time to make a plan. After choosing the destination city, the next step is to determine the travel route. As this poor guy has just experienced a tragic lost of money, he really has limited amount of money to spend. He wants to find the most costless route. Samball has just learned that the travel company will carry out a discount strategy during the vacation: the most expensive flight connecting two cities along the route will be free. This is really a big news.
Now given the source and destination cities, and the costs of all the flights, you are to calculate the minimum cost. It is assumed that the flights Samball selects will not have any cycles and the destination is reachable from the source.
Input
The input contains several test cases, each begins with a line containing names of the source city and the destination city. The next line contains an integer m (<=100), the number of flights, and then m lines follow, each contains names of the source city and the destination city of the flight and the corresponding cost. City names are composed of not more than 10 uppercase letters. Costs are integers between 0 to 10000 inclusively.
Process to the end of file.
Output
For each test case, output the minimum cost in a single line.
Sample Input
HANGZHOU BEIJING
2
HANGZHOU SHANGHAI 100
SHANGHAI BEIJING 200
Sample Output
100
#include<cstdio>#include<iostream>#include<cstring>#include<map>using namespace std;string src,dst,f1,f2;map<string,int> mp;const int inf=0x3f3f3f3f;int adj[210][210],maxx[210][210];int main(){ int n,w; while(cin>>src>>dst){ memset(adj,0x3f,sizeof adj); memset(maxx,0x3f,sizeof maxx); mp.clear(); scanf("%d",&n); int tot=0; for(int i=0;i<n;i++){ cin>>f1>>f2>>w; if(!mp.count(f1)) mp[f1]=tot++; if(!mp.count(f2)) mp[f2]=tot++; maxx[mp[f1]][mp[f2]]=w; adj[mp[f1]][mp[f2]]=0; } int t1=mp[src],t2=mp[dst]; int ans=adj[t1][t2]; for(int k=0;k<tot;k++) for(int i=0;i<tot;i++) for(int j=0;j<tot;j++){ int tmp=max(maxx[i][k],maxx[k][j]); if(tmp==inf) continue; if(maxx[i][k]>maxx[k][j]) maxx[i][j]=maxx[i][k]; else maxx[i][j]=maxx[k][j]; tmp=maxx[i][k]+maxx[k][j]-maxx[i][j]; adj[i][j]=min(adj[i][j],adj[i][k]+adj[k][j]+tmp); if(i==t1&&j==t2) ans=min(ans,adj[i][j]); } printf("%d\n",ans); }return 0;}
- ZOJ Problem Set - 2027
- ZOJ Problem Set - 2060
- ZOJ Problem Set - 2972
- ZOJ Problem Set - 1037
- ZOJ Problem Set - 1048
- ZOJ Problem Set - 1049
- ZOJ Problem Set - 1051
- ZOJ Problem Set - 1067
- ZOJ Problem Set - 1115
- ZOJ Problem Set - 1151
- ZOJ Problem Set - 1205
- ZOJ Problem Set - 1025
- ZOJ Problem Set - 1029
- ZOJ Problem Set - 1076
- ZOJ Problem Set - 1117
- ZOJ Problem Set - 1041
- ZOJ Problem Set - 1004
- ZOJ Problem Set - 1005
- __cdecl __stdcall区别
- UVA 11853 - Paintball(dfs)
- 乐元素面试
- Oracle -- Coalesce
- C++ 类非静态成员函数作为线程入口函数实现
- ZOJ Problem Set - 2027
- dbca建库不识别ASM存储
- c++类模板实例化验证
- nyoj15括号匹配(二)
- POJ Gold Balanced Lineup(3274)-神Hash
- 设计模式之(十) Bridge(桥接模式)
- webkit自定义进度条
- redis入门
- 第一次实验报告(1)