1111. Online Map (30)
Input our current position and a destination, an online map can recommend several paths. Now your job is to recommend two paths to your user: one is the shortest, and the other is the fastest. It is guaranteed that a path exists for any request.
Input Specification:
Each input file contains one test case. For each case, the first line gives two positive integers N (2 <= N <= 500), and M, being the total number of streets intersections on a map, and the number of streets, respectively. Then M lines follow, each describes a street in the format:
V1 V2 one-way length time
where V1 and V2 are the indices (from 0 to N-1) of the two ends of the street; one-way is 1 if the street is one-way from V1 to V2, or 0 if not; length is the length of the street; and time is the time taken to pass the street.
Finally a pair of source and destination is given.
Output Specification:
For each case, first print the shortest path from the source to the destination with distance D in the format:
Distance = D: source -> v1 -> ... -> destination
Then in the next line print the fastest path with total time T:
Time = T: source -> w1 -> ... -> destination
In case the shortest path is not unique, output the fastest one among the shortest paths, which is guaranteed to be unique. In case the fastest path is not unique, output the one that passes through the fewest intersections, which is guaranteed to be unique.
In case the shortest and the fastest paths are identical, print them in one line in the format:
Distance = D; Time = T: source -> u1 -> ... -> destination
Sample Input 1:10 15 0 1 0 1 1 8 0 0 1 1 4 8 1 1 1 3 4 0 3 2 3 9 1 4 1 0 6 0 1 1 7 5 1 2 1 8 5 1 2 1 2 3 0 2 2 2 1 1 1 1 1 3 0 3 1 1 4 0 1 1 9 7 1 3 1 5 1 0 5 2 6 5 1 1 2 3 5Sample Output 1:
Distance = 6: 3 -> 4 -> 8 -> 5 Time = 3: 3 -> 1 -> 5Sample Input 2:
7 9 0 4 1 1 1 1 6 1 1 3 2 6 1 1 1 2 5 1 2 2 3 0 0 1 1 3 1 1 1 3 3 2 1 1 2 4 5 0 2 2 6 5 1 1 2 3 5Sample Output 2:
Distance = 3; Time = 4: 3 -> 2 -> 5
题意:给你n个点和m条边,边可能为有向边,也可能为无向边,求两条路线:一条是最短距离的路线,若有多条最短距离的路线,输出最快的路线;一条是最快的路线,若有多条最快的路线,输出经过节点数量最少的。题目保证对任意的查询请求,地图上都至少存在一条可达路线。
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>
using namespace std;
#define LL long long
const int INF = 0x3f3f3f3f;
int cnt;
int s[600], nt[1000090], e[1000090];
int p1[600], p2[600], visit[600];
int dis[600], t[600],sum[600];
struct Edge
{
int dis, t;
}x[1000090];
struct node1
{
int id, dis;
friend bool operator <(node1 a, node1 b)
{
return a.dis>b.dis;
}
}pre1, nt1;
struct node2
{
int id, t;
friend bool operator <(node2 a, node2 b)
{
return a.t>b.t;
}
}pre2, nt2;
void Dijkstra1(int ss, int ee)
{
pre1.id = ss, pre1.dis = 0;
priority_queue<node1>q;
memset(visit, 0, sizeof visit);
memset(dis, INF, sizeof dis);
memset(t, INF, sizeof t);
t[ss] = 0, dis[ss] = 0;
q.push(pre1);
while (!q.empty())
{
pre1 = q.top();
q.pop();
visit[pre1.id] = 1;
if (pre1.id == ee) break;
for (int i = s[pre1.id]; ~i; i = nt[i])
{
int ee = e[i];
if (visit[ee]) continue;
if (dis[ee]>dis[pre1.id] + x[i].dis)
{
dis[ee] = dis[pre1.id] + x[i].dis;
t[ee] = t[pre1.id] + x[i].t;
p1[ee] = pre1.id;
nt1.dis = dis[ee];
nt1.id = ee;
q.push(nt1);
}
else if (dis[ee] == dis[pre1.id] + x[i].dis&&t[ee]>t[pre1.id] + x[i].t)
{
sum[ee] = sum[pre1.id] + x[i].t;
p1[ee] = pre1.id;
}
}
}
}
void Dijkstra2(int ss, int ee)
{
pre2.id = ss, pre2.t = 0;
priority_queue<node2>q;
memset(visit, 0, sizeof visit);
memset(t, INF, sizeof t);
memset(sum, INF, sizeof sum);
t[ss] = 0, sum[ss] = 0;
q.push(pre2);
while (!q.empty())
{
pre2 = q.top();
q.pop();
visit[pre2.id] = 1;
if (pre2.id == ee) break;
for (int i = s[pre2.id]; ~i; i = nt[i])
{
int ee = e[i];
if (visit[ee]) continue;
if (t[ee]>t[pre2.id] + x[i].t)
{
t[ee] = t[pre2.id] + x[i].t;
sum[ee] = sum[pre2.id] + 1;
p2[ee] = pre2.id;
nt2.t = t[ee];
nt2.id = ee;
q.push(nt2);
}
else if (t[ee] == t[pre2.id] + x[i].t&&sum[ee]>sum[pre2.id] + 1)
{
sum[ee] = sum[pre2.id] + 1;
p2[ee] = pre2.id;
}
}
}
}
int main()
{
int n, m;
while (~scanf("%d %d", &n, &m))
{
cnt = 1;
memset(s, -1, sizeof s);
memset(nt, -1, sizeof nt);
int u, v, k, l, time;
for (int i = 1; i <= m; i++)
{
scanf("%d %d %d %d %d", &u, &v, &k, &l, &time);
nt[cnt] = s[u], s[u] = cnt, e[cnt] = v, x[cnt].dis = l, x[cnt++].t = time;
if (k == 0) nt[cnt] = s[v], s[v] = cnt, e[cnt] = u, x[cnt].dis = l, x[cnt++].t = time;
}
scanf("%d %d", &u, &v);
Dijkstra1(u, v);
Dijkstra2(u, v);
int kk = v, flag = 1;
while (p1[kk] != u)
{
if (p1[kk] != p2[kk]) { flag = 0; break; }
kk = p1[kk];
}
if (flag)
{
printf("Distance = %d; Time = %d: %d", dis[v], t[v], u);
stack<int>ss;
kk = v;
while (kk != u) { ss.push(kk); kk = p1[kk]; }
while (!ss.empty()) { printf(" -> %d", ss.top()); ss.pop(); }
}
else
{
printf("Distance = %d: %d", dis[v], u);
stack<int>ss;
kk = v;
while (kk != u) { ss.push(kk); kk = p1[kk]; }
while (!ss.empty()) { printf(" -> %d", ss.top()); ss.pop(); }
printf("\n");
printf("Time = %d: %d", t[v], u);
kk = v;
while (kk != u) { ss.push(kk); kk = p2[kk]; }
while (!ss.empty()) { printf(" -> %d", ss.top()); ss.pop(); }
}
printf("\n");
}
return 0;
}