uva 11374 Airport Express(最短路dijistra+堆优化,4级)

Description

Problem D: Airport Express

In a small city called Iokh, a train service, Airport-Express, takes residents to the airport more quickly than other transports. There are two types of trains in Airport-Express, the Economy-Xpress and theCommercial-Xpress. They travel at different speeds, take different routes and have different costs.

Jason is going to the airport to meet his friend. He wants to take the Commercial-Xpress which is supposed to be faster, but he doesn't have enough money. Luckily he has a ticket for the Commercial-Xpress which can take him one station forward. If he used the ticket wisely, he might end up saving a lot of time. However, choosing the best time to use the ticket is not easy for him.

Jason now seeks your help. The routes of the two types of trains are given. Please write a program to find the best route to the destination. The program should also tell when the ticket should be used.

Input

The input consists of several test cases. Consecutive cases are separated by a blank line.

The first line of each case contains 3 integers, namely NS and E (2 ≤ N ≤ 500, 1 ≤ SE ≤ N), which represent the number of stations, the starting point and where the airport is located respectively.

There is an integer M (1 ≤ M ≤ 1000) representing the number of connections between the stations of the Economy-Xpress. The next Mlines give the information of the routes of the Economy-Xpress. Each consists of three integers XY and Z (XY ≤ N, 1 ≤ Z ≤ 100). This means X and Y are connected and it takes Z minutes to travel between these two stations.

The next line is another integer K (1 ≤ K ≤ 1000) representing the number of connections between the stations of the Commercial-Xpress. The next K lines contain the information of the Commercial-Xpress in the same format as that of the Economy-Xpress.

All connections are bi-directional. You may assume that there is exactly one optimal route to the airport. There might be cases where you MUST use your ticket in order to reach the airport.

Output

For each case, you should first list the number of stations which Jason would visit in order. On the next line, output "Ticket Not Used" if you decided NOT to use the ticket; otherwise, state the station where Jason should get on the train of Commercial-Xpress. Finally, print the total time for the journey on the last line. Consecutive sets of output must be separated by a blank line.

Sample Input

4 1 4
4
1 2 2
1 3 3
2 4 4
3 4 5
1
2 4 3

Sample Output

1 2 4
2
5

Problemsetter: Raymond Chun
Originally appeared in CXPC, Feb. 2004

思路:最短路,预处理,求出S出发的所有最短点距hd[],E点出发的最小点距td[]

           答案就是min(hd[a]+td[b]+dis[a][b]),dis[a][b]为商务距离。

           最短路,用SPFA,或者dijistra都行,本题明显dijistra好一点


#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
const int mm=500+9;
const int oo=1e9;
const int mn=2000+9;
int n,S,E,m;
int head[mm],edge;
class node
{public:int v,next,dis;
}e[mn];
class heap
{
	public:int d,u;
	bool operator <(const heap& x)const
	{return d>x.d;
	}
};
class Edge
{public:int u,v,dis;
}ee[mn],ef[mn];
void data()
{memset(head,-1,sizeof(head));
edge=0;
}
void add(int u,int v,int dist)
{
 e[edge].v=v;e[edge].dis=dist;e[edge].next=head[u];head[u]=edge++;
}
int hd[mm],td[mm],hpath[mm],tpath[mm];bool vis[mm];
void dijkstra(int*d,int *path,int S)
{
	priority_queue<heap>Q;
	memset(vis,0,sizeof(vis));
	for(int i=0;i<=n;++i)
    d[i]=oo,path[i]=-1;
	d[S]=0;
	Q.push((heap){0,S});
	int u,v;
	while(!Q.empty())
	{
		heap z=Q.top();Q.pop();
		if(vis[z.u])continue;
		u=z.u;vis[z.u]=1;
		for(int i=head[u];~i;i=e[i].next)
		{
			v=e[i].v;
			if(d[v]>d[u]+e[i].dis)
			{d[v]=d[u]+e[i].dis;
			 path[v]=u;
			 Q.push((heap){d[v],v});
			}
		}
	}
}
void print(int x)
{  if(hpath[x]!=-1)
	print(hpath[x]);
	if(x!=E)
		printf("%d ",x);
	else printf("%d\n",x);
}
int main()
{  int a,b,c,cas=0;
	while(~scanf("%d%d%d",&n,&S,&E))
	{ if(cas)printf("\n");++cas;
		scanf("%d",&m);
		data();
		for(int i=0;i<m;++i)
		{
			scanf("%d%d%d",&a,&b,&c);
			ee[i].u=a;ee[i].v=b;ee[i].dis=c;
		    add(a,b,c);add(b,a,c);
		}
		dijkstra(hd,hpath,S);
		//for(int i=1;i<=n;++i)
    //  cout<<i<<" "<<hd[i]<<endl;
		data();
		for(int i=0;i<m;++i)
			add(ee[i].u,ee[i].v,ee[i].dis),add(ee[i].v,ee[i].u,ee[i].dis);
		dijkstra(td,tpath,E);
		scanf("%d",&m);
		int ans=hd[E],ticket=0,to;
		for(int i=0;i<m;++i)
		{
          scanf("%d%d%d",&a,&b,&c);
		  if(ans>hd[a]+td[b]+c)
		  {ticket=a;ans=hd[a]+td[b]+c;to=b;
		  }
		   if(ans>hd[b]+td[a]+c)
		  {
			  ticket=b;ans=hd[b]+td[a]+c;to=a;
		  }
		}
	  int x=S;
	  if(!ticket)
	  print(E);
	  else {print(ticket);
	   x=to;
	   while(x!=E)
	   {printf("%d ",x);x=tpath[x];
	   }
	   printf("%d\n",E);
	  }
	  if(!ticket)printf("Ticket Not Used\n");
	  else printf("%d\n",ticket);
	  printf("%d\n",ans);
	}
	return 0;
}




你可能感兴趣的:(uva 11374 Airport Express(最短路dijistra+堆优化,4级))