HDU 3499 Flight (SPFA+分层图)

xiaoxiao2021-02-27  324

Flight

Time Limit : 20000/10000ms (Java/Other)   Memory Limit : 65535/65535K (Java/Other)
Total Submission(s) : 14   Accepted Submission(s) : 3

Font: Times New Roman | Verdana | Georgia

Font Size:  

Problem Description

Recently, Shua Shua had a big quarrel with his GF. He is so upset that he decides to take a trip to some other city to avoid meeting her. He will travel only by air and he can go to any city if there exists a flight and it can help him reduce the total cost to the destination. There's a problem here: Shua Shua has a special credit card which can reduce half the price of a ticket ( i.e. 100 becomes 50, 99 becomes 49. The original and reduced price are both integers. ). But he can only use it once. He has no idea which flight he should choose to use the card to make the total cost least. Can you help him?

Input

There are no more than 10 test cases. Subsequent test cases are separated by a blank line.  The first line of each test case contains two integers N and M ( 2 <= N <= 100,000  0 <= M <= 500,000 ), representing the number of cities and flights. Each of the following M lines contains "X Y D" representing a flight from city X to city Y with ticket price D ( 1 <= D <= 100,000 ). Notice that not all of the cities will appear in the list! The last line contains "S E" representing the start and end city. X, Y, S, E are all strings consisting of at most 10 alphanumeric characters. 

Output

One line for each test case the least money Shua Shua have to pay. If it's impossible for him to finish the trip, just output -1.

Sample Input

4 4 Harbin Beijing 500 Harbin Shanghai 1000 Beijing Chengdu 600 Shanghai Chengdu 400 Harbin Chengdu 4 0 Harbin Chengdu

Sample Output

800 -1

Hint

In the first sample, Shua Shua should use the card on the flight from Beijing to Chengdu, making the route Harbin->Beijing->Chengdu have the least total cost 800. In the second sample, there's no way for him to get to  Chengdu from Harbin, so -1 is needed.

Author

Edelweiss

Source

2010 ACM-ICPC Multi-University Training Contest(7)——Host by HIT 题意:求最短路,但是其中任意一条路的权值可以变一半。 POINT:掌握分层图就好啦!而且时间有10S,随便来。 longlong #include<stdio.h> #include <queue> #include <iostream> #include <string.h> #include <map> using namespace std; #define ll long long const int N = 1500000+10; const ll inf =0x3f3f3f3f3f3f3f3fll; int be[N],ed[N]; ll w[N]; ll dis[300000+6]; int n,m; map<string ,int >name; int fir[N],nxt[N],vis[N]; void spfa(int b) { for(int i=1; i<=3*n; i++) { dis[i]=inf; } dis[b]=0; memset(vis,0,sizeof vis); vis[b]=1; queue<int> q; while(!q.empty()) q.pop(); q.push(b); while(!q.empty()) { int now=q.front(); q.pop(); vis[now]=0; int k=fir[now]; while(k!=-1) { if(dis[ed[k]]>=dis[be[k]]+w[k]) { dis[ed[k]]=dis[be[k]]+w[k]; if(vis[ed[k]]==0) vis[ed[k]]=1,q.push(ed[k]); } k=nxt[k]; } } } void displayway() { for(int i=1; i<=m; i++) { printf("%d %d %lld\n",be[i],ed[i],w[i]); } } int cnt=0; void add(int a,int b,ll x) { cnt++; be[cnt]=a; ed[cnt]=b; w[cnt]=x; nxt[cnt]=fir[a]; fir[a]=cnt; } int main() { while(~scanf("%d %d",&n,&m)) { memset(be,0,sizeof be); memset(ed,0,sizeof ed); memset(w,0,sizeof w); name.clear(); memset(fir,-1,sizeof fir); memset(nxt,-1,sizeof nxt); int num=0; cnt=0; for(int i=1; i<=m; i++) { char s1[20],s2[20]; ll c; scanf("%s %s %lld",s1,s2,&c); name[s1]=name[s1]==0?++num:name[s1]; name[s2]=name[s2]==0?++num:name[s2]; //A层可以到B层和A层。 //B层只能到C层。 //C层只能到C层 add(name[s1]+n,name[s2]+n,c);//A层 add(name[s1]+n,name[s2],c/2);//B层 add(name[s1],name[s2],c);//C层 } char s1[20],s2[20]; scanf("%s %s",s1,s2); name[s1]=name[s1]==0?++num:name[s1]; name[s2]=name[s2]==0?++num:name[s2]; spfa(name[s1]+n);//从A层开始 if(dis[name[s2]]==inf) printf("-1\n");//C层结束 else printf("%lld\n",dis[name[s2]]); } return 0; }
转载请注明原文地址: https://www.6miu.com/read-5610.html

最新回复(0)