dijkstra+枚举+恶心的格式.....
Airport Express
[Submit] [Go Back] [Status] Description 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. InputThe 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 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. OutputFor 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 Input4 1 4 4 1 2 2 1 3 3 2 4 4 3 4 5 1 2 4 3 Sample Output1 2 4 2 5 Problemsetter: Raymond Chun Originally appeared in CXPC, Feb. 2004 [Submit] [Go Back] [Status] |
#include
#include
#include
#include
#include
using namespace std;
const int maxn=12000,INF=0x3f3f3f3f;
typedef pair pII;
typedef pair pIII;
struct Edge
{
int to,next,cost;
}edge[maxn];
int Adj[maxn],Size;
void init()
{
Size=0; memset(Adj,-1,sizeof(Adj));
}
void Add_Edge(int u,int v,int c)
{
edge[Size].to=v;
edge[Size].cost=c;
edge[Size].next=Adj[u];
Adj[u]=Size++;
}
int dist_S[maxn],dist_E[maxn],S,E;
int pre_S[maxn],pre_E[maxn];
bool vis[maxn];
int n,M,K;
pIII CX[maxn];
void dijkstra_S()
{
memset(dist_S,63,sizeof(dist_S));
memset(vis,false,sizeof(vis));
memset(pre_S,-1,sizeof(pre_S));
dist_S[S]=0;
for(int i=1;i<=n;i++)
{
int mark=-1,mindist=INF;
for(int j=1;j<=n;j++)
{
if(!vis[j]&&dist_S[j]dist_S[mark]+edge[j].cost)
{
dist_S[u]=dist_S[mark]+edge[j].cost;
pre_S[u]=mark;
}
}
}
}
}
void dijkstra_E()
{
memset(dist_E,63,sizeof(dist_E));
memset(vis,false,sizeof(vis));
memset(pre_E,-1,sizeof(pre_E));
dist_E[E]=0;
for(int i=1;i<=n;i++)
{
int mark=-1,mindist=INF;
for(int j=1;j<=n;j++)
{
if(!vis[j]&&dist_E[j]dist_E[mark]+edge[j].cost)
{
dist_E[u]=dist_E[mark]+edge[j].cost;
pre_E[u]=mark;
}
}
}
}
}
vector road;
void next_S(int x)
{
road.push_back(x);
if(pre_S[x]!=-1) next_S(pre_S[x]);
}
void next_E(int x)
{
road.push_back(x);
if(pre_E[x]!=-1) next_E(pre_E[x]);
}
int main()
{
int cas=0;
while(scanf("%d%d%d",&n,&S,&E)!=EOF)
{
init();
scanf("%d",&M);
for(int i=0;itemp)
{
ST=u; ED=v;
ans=temp; CE=i;
}
temp=dist_S[v]+c+dist_E[u];
if(ans>temp)
{
ST=v; ED=u;
ans=temp; CE=i;
}
}
road.clear();
if(ST==-1)
{
next_S(E);
int sz=road.size();
for(int i=sz-1;i>=0;i--)
{
if(i!=sz-1) putchar(32);
printf("%d",road[i]);
}
}
else
{
next_S(ST);
int sz=road.size();
for(int i=sz-1;i>=0;i--)
{
if(i!=sz-1) putchar(32);
printf("%d",road[i]);
}
road.clear();
next_E(ED);
sz=road.size();
for(int i=0;i