zoj 2027 Travelling Fee (最短路变形)
来源:互联网 发布:在迪拜学英语 知乎 编辑:程序博客网 时间:2024/06/04 08:49
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"stdio.h"#include"string.h"#include"vector"#include"queue"#include"iostream"#include"algorithm"using namespace std;#define N 205const int inf=(int)1e10;int g[N][N],dis[N];bool mark[N];int n,ans;void inti(){ int i,j; for(i=0;i<N;i++) { for(j=0;j<N;j++) g[i][j]=(i==j?0:inf); }}void dijkstra(int s,int t){ int i,min,sum=0; memset(mark,0,sizeof(mark)); mark[s]=1; for(i=0;i<n;i++) dis[i]=g[s][i]; dis[s]=0; while(1) { s=0; min=inf; for(i=0;i<n;i++) { if(!mark[i]&&min>dis[i]) { min=dis[i]; s=i; } } if(s==0) break; sum+=min; mark[s]=1; if(s==t) break; for(i=0;i<n;i++) { if(!mark[i]&&dis[i]>dis[s]+g[s][i]) dis[i]=g[s][i]; } } ans=ans<sum?ans:sum;}int main(){ int i,j,k,m,a,b,d; char str[N][15],s1[15],s2[15]; while(scanf("%s %s",s1,s2)!=-1) { inti(); strcpy(str[0],s1); strcpy(str[1],s2); n=2; scanf("%d",&m); while(m--) //输入边 { scanf("%s %s %d",s1,s2,&d); for(i=0;i<n;i++) if(strcmp(str[i],s1)==0) break; if(i==n) strcpy(str[n++],s1); a=i; for(i=0;i<n;i++) if(strcmp(str[i],s2)==0) break; if(i==n) strcpy(str[n++],s2); b=i; g[a][b]=d; } ans=inf; for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(i!=j&&g[i][j]<inf) //枚举每条边置为零 { int t=g[i][j]; g[i][j]=0; dijkstra(0,1); g[i][j]=t; } } } printf("%d\n",ans); } return 0;}
- zoj 2027 Travelling Fee (最短路变形)
- zoj 2027 Travelling Fee(最短路)
- ZOJ 2027 Travelling Fee(最短路)
- ZOJ 2027 Travelling Fee
- ZOJ:2027 Travelling Fee
- ZOJ 2027 Travelling Fee
- ZOJ-2027 Travelling Fee
- zoj 2027 Travelling Fee
- zoj 2027 Travelling Fee
- ZJU2027 Travelling Fee - 最短路径
- ZOJ 1655 Transport Goods 最短路变形
- zoj 2027 Travelling Fee ( 两种解法 1.SPFA 2.floyd()+dp )
- ZOJ 2027 最短路
- zoj 1655 Transport Goods (最短路变形)
- zoj 3781 Paint the Grid Reloaded 最短路变形
- 最短路变形_Poj_2253
- 最短路变形ural1934
- poj1797 最短路变形
- C#write和writeline的区别 read和readline的区别
- java经典编程题(16-18)
- php 使用url 中文乱码问题
- java经典编程题(19-21)
- 你可以选择自己想过的生活
- zoj 2027 Travelling Fee (最短路变形)
- Linux C 函数可变参数处理
- hdu1599 floyd 求最小环
- android EditText输入框获得焦点时,边框变为深色
- public,private,protected 区别
- 二叉树的输入、遍历、与线索化
- 【COM编程】从C++到COM,学习笔记
- java经典编程题(22-24)
- java经典编程题(25-27)