题意:去机场有经济线和商业线这两种火车可以选择,现在你有一张商业线的火车票,可以坐一站,其余时候只能坐经济线,问去机场的最快路线,并且打印路径
思路:我们假设从a到b坐的是商业线,那么最短时间就是从起点出发到a的最短路径+a到b的时间+b到终点的最短路径,首先对起点和终点分别跑一遍最短路,然后枚举每一点,维护ans最少,路径递归输出即可
Trick:这个题输出格式真的是要醉了..两个样例之间输出空格,而且不会判PE..会直接WA
#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
using namespace std;
#define maxn 200005
#define LL long long
int cas=0,T;
int n,S,E;
const int INF = 0x3f3f3f3f ;
struct Edge
{
int from,to,dist;
Edge(){}
Edge(int u,int v,int d):from(u),to(v),dist(d){}
};
struct HeapNode
{
int d,u;
bool operator < (const HeapNode &rhs)const
{
return d > rhs.d;
}
};
vector<Edge>edges;
vector<int> G[maxn];
int d[maxn];
int vis[maxn];
int p[maxn];
int p1[maxn];
int p2[maxn];
void init()
{
for (int i = 0;i<=n;i++)
G[i].clear();
edges.clear();
}
void AddEdge(int from,int to,int dist)
{
edges.push_back(Edge(from,to,dist));
int mm = edges.size();
G[from].push_back(mm-1);
}
void dijkstra(int s)
{
priority_queue<HeapNode> q;
for (int i = 1;i<=n;i++)
d[i]=INF;
d[s]=0;
memset(vis,0,sizeof(vis));
memset(p,0,sizeof(p));
q.push((HeapNode){0,s});
while (!q.empty())
{
HeapNode x = q.top();q.pop();
int u = x.u;
if (vis[u])continue;
for (int i = 0;i<G[u].size();i++)
{
Edge &e = edges[G[u][i]];
if (d[e.to] > d[u] + e.dist)
{
d[e.to] = d[u] + e.dist;
p[e.to] = G[u][i];
q.push((HeapNode){d[e.to],e.to});
}
}
}
}
void PrintA(int k)
{
if (k==S)
{
printf("%d",k);
return ;
}
int temp = p1[k];
Edge &e = edges[temp];
PrintA(e.from);
printf(" %d",k);
}
void PrintB(int k)
{
if (k == E)
{
printf(" %d",k);
return ;
}
printf(" %d",k);
int temp = p2[k];
Edge &e = edges[temp];
PrintB(e.from);
}
int m;
int f[maxn];
int g[maxn];
int main()
{
//freopen("in","r",stdin);
int s;
while (scanf("%d%d%d",&n,&S,&E)!=EOF)
{
if (cas++)
printf("\n");
scanf("%d",&m);
init();
for (int i = 0;i<m;i++)
{
int u,v,d;
scanf("%d%d%d",&u,&v,&d);
AddEdge(u,v,d);
AddEdge(v,u,d);
}
dijkstra(S);
for (int i = 1;i<=n;i++)
{
f[i] = d[i];
p1[i] = p[i];
}
dijkstra(E);
for (int i = 1;i<=n;i++)
{
g[i] = d[i];
p2[i] = p[i];
}
int k;
scanf("%d",&k);
int ans = f[E];
int st = -1,ed = -1;
for (int i = 0;i<k;i++)
{
int u,v,d;
scanf("%d%d%d",&u,&v,&d);
if (ans > f[u]+d+g[v])
{
ans = f[u]+d+g[v];
st = u;
ed = v;
}
if (ans > f[v]+d+g[u])
{
ans = f[v]+d+g[u];
st = v;
ed = u;
}
}
if (st == -1)
{
PrintA(E);
printf("\nTicket Not Used\n");
}
else
{
PrintA(st);
PrintB(ed);
printf("\n%d\n",st);
}
printf("%d\n",ans);
}
//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
return 0;
}
题目
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. Theytravel at different speeds, take different routes and have differentcosts.Jason is going to the airport to meet his friend. He wantsto take the Commercial-Xpress which is supposed to be faster,but he doesn’t have enough money. Luckily he has a ticketfor the Commercial-Xpress which can take him one stationforward. If he used the ticket wisely, he might end up savinga lot of time. However, choosing the best time to use theticket is not easy for him.Jason now seeks your help. The routes of the two typesof trains are given. Please write a program to find the bestroute 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 stationsof 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 areconnected 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 betweenthe stations of the Commercial-Xpress. The next K lines contain the information of the CommercialXpressin 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 theairport. 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 thenext line, output ‘Ticket Not Used’ if you decided NOT to use the ticket; otherwise, state the stationwhere Jason should get on the train of Commercial-Xpress. Finally, print the total time for the journeyon 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