您的位置:首页 > 其它

1111. Online Map (30)

2018-03-07 20:29 375 查看
1111. Online Map (30)

时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B

判题程序 Standard 作者 CHEN, Yue

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 5

Sample Output 1:

Distance = 6: 3 -> 4 -> 8 -> 5

Time = 3: 3 -> 1 -> 5

Sample 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 5

Sample Output 2:

Distance = 3; Time = 4: 3 -> 2 -> 5

坑点:时间相同的输出节点最少的,不是距离最短的,7分的测试点

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <queue>

using namespace std;
const int MaxN = 600;
const int INF = INT32_MAX;
int GD[MaxN][MaxN], GT[MaxN][MaxN], N, M, MinT = INF, MinL = INF;
int trecord[MaxN], lrecord[MaxN];
vector<int>lpre[MaxN], tpre[MaxN], tmppath, spath, tpath;
bool isInq[MaxN];

void SFPA(int st, int G[][MaxN], vector<int>pre[],int des[]) {
fill(des, des + N + 2, INF);
memset(isInq, 0, N + 2);
des[st] = 0;
queue<int>que; que.push(st);
while (que.size()) {
int u = que.front(); que.pop(); isInq[u] = false;
for (int v = 0; v < N; ++v) {
if (G[u][v] != INF) {
if (des[v] > des[u] + G[u][v]) {
des[v] = des[u] + G[u][v];
pre[v].clear();
pre[v].push_back(u);
if (!isInq[v]) {
que.push(v);
isInq[v] = true;
}
}
else if (des[v] == des[u] + G[u][v]) pre[v].push_back(u);
}
}
}
}

void DFSL(int v, int st, int G[][MaxN], vector<int>pre[],vector<int>&path ,int &Min) {
tmppath.push_back(v);
if (v == st) {
int tmpMin = 0;
for (int i = tmppath.size() - 1; i > 0; --i) {
int u = tmppath[i], v = tmppath[i - 1];
tmpMin += G[u][v];
}

if (tmpMin < Min) {
Min = tmpMin;
path = tmppath;
}
}
for (int i = 0; i < pre[v].size(); ++i) DFSL(pre[v][i], st, G, pre, path, Min);
tmppath.pop_back();
}

void DFST(int v, int st, int G[][MaxN], vector<int>pre[], vector<int>&path, int &Min) {
tmppath.push_back(v);
if (v == st) {
if (tmppath.size() < Min) {
Min = tmppath.size();
path = tmppath;
}
}
for (int i = 0; i < pre[v].size(); ++i) DFST(pre[v][i], st, G, pre, path, Min);
tmppath.pop_back();
}

void printPath(vector<int>path) {
for (int i = path.size() - 1; i >= 0; --i) {
printf("%d", path[i]);
if (i)printf(" -> ");
}
}

int main() {
#ifdef _DEBUG
freopen("data.txt", "r+", stdin);
#endif // _DEBUG

int u, v, oneway, l, t, st, ed;
scanf("%d %d", &N, &M);
fill(GD[0], GD[0] + (N + 2) * MaxN, INF);
fill(GT[0], GT[0] + (N + 2) * MaxN, INF);
for (int i = 0; i < M; ++i) {
scanf("%d %d %d %d %d", &u, &v, &oneway, &l, &t);
GD[u][v] = l; GT[u][v] = t;
if (oneway == 0) {
GD[v][u] = l;
GT[v][u] = t;
}
}
scanf("%d %d", &st, &ed);
SFPA(st, GD, lpre, lrecord);
SFPA(st, GT, tpre, trecord);

DFSL(ed, st, GT, lpre, spath, MinT);
DFST(ed, st, GD, tpre, tpath, MinL);

if (spath == tpath) {
printf("Distance = %d; Time = %d: ", lrecord[ed], trecord[ed]);
printPath(spath);
}
else {
printf("Distance = %d: ", lrecord[ed]);
printPath(spath);
printf("\nTime = %d: ", trecord[ed]);
printPath(tpath);
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  pat甲级 1111