[UVA 11374]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 the Commercial-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 N, S and E (2 ≤ N ≤ 500, 1 ≤ S, E ≤ 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 M lines give the information of the routes of the Economy-Xpress. Each consists of three integers X, Y and Z (X, Y ≤ 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 CommercialXpress 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                        //最少花費

【中文大意】

N個點,從起點S到終點E,有M條經濟和K條商業線,只能乘坐一 次商業線,求最短時間。

【格式注意】

1.多組數據

2.每兩組數據之間空一行

第一組
(換行)
第二組
(換行)
......
最後一組

3.行末不能有多餘空格

【題解】

1.

(勉勉強強的......)先用所有的經濟線建圖;每次加入一條商業線跑最短路(Dijkstra+堆優化)。

2.

正向、反向各跑一邊最短路,分別以S,E爲起點。然後每次加入一條邊更新最小答案。

【代碼(2)】

#include <bits/stdc++.h>
using namespace std;
int n,S,E;
int m,k;
int lin[555]={};
struct kk
{
	int id;
	int next;
	int v;
} e[2222]={};
int num;
void insert(int x,int y,int v)
{
	e[++num].next=lin[x];
	lin[x]=num;
	e[num].id=y;
	e[num].v=v;
}
priority_queue< pair<int,int> > q;
int d[555]={};
int dis[555]={};
int vis[555]={};
int fa[555]={};
int son[555]={};
void dij(int x)
{
	memset(vis,0,sizeof(vis));
	memset(d,10,sizeof(d));
	q.push(make_pair(0,x));
	d[x]=0;
	for(;!q.empty();)
	{
	    int top=q.top().second;
		q.pop();
		if(vis[top])
		 continue;
		vis[top]=1;
		for(int i=lin[top];i;i=e[i].next)
		{
			if(d[top]+e[i].v<d[e[i].id])
			{
			    d[e[i].id]=d[top]+e[i].v;
			    q.push(make_pair(-d[e[i].id],e[i].id));
			    if(x==S)
			     fa[e[i].id]=top;
			    else
			     son[e[i].id]=top;
			}
		}
	}
}
void find(int x)
{
	if(x!=S&&x)
	 find(fa[x]);
	if(x!=E)
	 printf("%d ",x);
	else
	 printf("%d\n",x);
}
int o=0;
void clear_()
{
	memset(lin,0,sizeof(lin));
	memset(e,0,sizeof(e));
	memset(dis,10,sizeof(dis));
	memset(fa,0,sizeof(fa));
	memset(son,0,sizeof(son));
	num=0;
}
int main()
{   
      for(;scanf("%d%d%d",&n,&S,&E)!=EOF;)
	  {
	  	clear_();//初始化
		num=0;
		scanf("%d",&m);
		for(int i=1;i<=m;i++)
		{
			int x,y,v;
			scanf("%d%d%d",&x,&y,&v);
			insert(x,y,v);
			insert(y,x,v);//建圖
		}
		dij(S);//正向最短路
		for(int i=1;i<=n;i++)
		 dis[i]=d[i];//記錄
		dij(E);//反向最短路
		int ans=dis[E];//記錄不加入商業線時的最小答案
		int X,Y;
		X=Y=E;
		cin>>k;
		for(int i=1;i<=k;i++)
		{
			int x,y,v;
			scanf("%d%d%d",&x,&y,&v);
			if(dis[x]+d[y]+v<ans)
			{
			    ans=dis[x]+d[y]+v;
			    X=x;
			    Y=y;
			}
			if(d[x]+dis[y]+v<ans)
			{
				ans=d[x]+dis[y]+v;
				X=y;
				Y=x;
			}//加入商業線進行判斷
		}
	  	if(o)
	  	 cout<<endl;//換行
	  	o++;
		find(X);
		if(X!=E)
		{
		    if(Y!=E)
		     cout<<Y<<' ';
		    for(int i=son[Y];i!=E&&i;i=son[i])
		     cout<<i<<' ';
		    cout<<E<<endl;
	    }//輸出(1)
		if(X!=Y)
		 printf("%d\n",X);
		else
		 printf("Ticket Not Used\n");//輸出(2)
		printf("%d\n",ans);//輸出(3)
	}
	return 0;
 } 

【小結】

細節處理很重要。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章