标签:des style http color os io strong for ar
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<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<map> #include<vector> #include<cmath> #include<string> #include<queue> #define eps 1e-9 #define ll long long #define INF 0x3f3f3f3f using namespace std; const int maxn=100+10; const int maxm=maxn*2+10; map<string,bool>Judge; map<string,int>mp; struct Edge { int to,val,next; }edge[maxn],temp[maxn]; int st,en,n,m,cnt,cal; int dis[maxm],head[maxm]; bool vis[maxm]; char start[maxm],End[maxm]; queue<int>Q; int Spfa(int k) { while(!Q.empty()) Q.pop(); memset(vis,false,sizeof(vis)); memset(dis,0x3f,sizeof(dis)); for(int i=1;i<=cnt;i++) temp[i]=edge[i]; temp[k].val=0; dis[1]=0; vis[1]=true; Q.push(1); while(!Q.empty()) { int Gery=Q.front(); Q.pop(); for(int i=head[Gery];i!=-1;i=temp[i].next) { int small_milan=temp[i].to; int val=temp[i].val; if(dis[small_milan]>dis[Gery]+val) { dis[small_milan]=dis[Gery]+val; if(!vis[small_milan]) { vis[small_milan]=true; Q.push(small_milan); } } } } return dis[2]; } void add_edge(int x,int y,int val) { edge[++cnt].to=y; edge[cnt].val=val; edge[cnt].next=head[x]; head[x]=cnt; } void read_Graph() { int w; memset(head,-1,sizeof(head)); scanf("%d",&m); for(int i=1;i<=m;i++) { scanf("%s%s%d",start,End,&w); if(!Judge[start]) { Judge[start]=true; mp[start]=++cal; } if(!Judge[End]) { Judge[End]=true; mp[End]=++cal; } add_edge(mp[start],mp[End],w); add_edge(mp[End],mp[start],w); } } int main() { while(~scanf("%s%s",start,End)) { Judge.clear(); mp.clear(); cal=cnt=0; Judge[start]=true; Judge[End]=true; mp[start]=++cal; mp[End]=++cal; read_Graph(); int ans=INF; for(int i=1;i<=cnt;i++) ans=min(ans,Spfa(i)); printf("%d\n",ans); } return 0; } /* a b 3 a b 2 b c 5 a c 8 */
zoj2027Travelling Fee(Spfa+枚举)
标签:des style http color os io strong for ar
原文地址:http://blog.csdn.net/u014303647/article/details/38894091