Description
Bessie and her sister Elsie want to travel from the barn to their favorite field, such that they leave at exactly the same time from the barn, and also arrive at exactly the same time at their favorite field. The farm is a collection of N fields (1 <= N <= 100) numbered 1..N, where field 1 contains the barn and field N is the favorite field. The farm is built on the side of a hill, with field X being higher in elevation than field Y if X < Y. An assortment of M paths connect pairs of fields. However, since each path is rather steep, it can only be followed in a downhill direction. For example, a path connecting field 5 with field 8 could be followed in the 5 -> 8 direction but not the other way, since this would be uphill. Each pair of fields is connected by at most one path, so M <= N(N-1)/2. It might take Bessie and Elsie different amounts of time to follow a path; for example, Bessie might take 10 units of time, and Elsie 20. Moreover, Bessie and Elsie only consume time when traveling on paths between fields – since they are in a hurry, they always travel through a field in essentially zero time, never waiting around anywhere. Please help determine the shortest amount of time Bessie and Elsie must take in order to reach their favorite field at exactly the same moment.
给出一个n个点m条边的有向无环图,每条边两个边权。
n<=100,没有重边。
然后要求两条长度相同且尽量短的路径,
路径1采用第一种边权,路径2采用第二种边权。
没有则输出”IMPOSSIBLE”
Input
The first input line contains N and M, separated by a space. Each of the following M lines describes a path using four integers A B C D, where A and B (with A < B) are the fields connected by the path, C is the time required for Bessie to follow the path, and D is the time required for Elsie to follow the path. Both C and D are in the range 1..100.
Output
A single integer, giving the minimum time required for Bessie and Elsie to travel to their favorite field and arrive at the same moment. If this is impossible, or if there is no way for Bessie or Elsie to reach the favorite field at all, output the word IMPOSSIBLE on a single line.
Sample Input
3 3
1 3 1 2
1 2 1 2
2 3 1 2
Sample Output
2
SOLUTION NOTES:
Bessie is twice as fast as Elsie on each path, but if Bessie takes the path 1->2->3 and Elsie takes the path 1->3 they will arrive at the same time.
Solution
用奇怪的姿势A掉了。
分别找出两种途径的前20短路,比较一下有相同的输出即可。
#include <bits/stdc++.h>
#define INF 0x7f7f7f7f
using namespace std;
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
while (ch >= '0'&& ch <= '9') { x = x * 10 + ch - '0'; ch = getchar();}
return x * f;
}
struct Edge{
int to, v[2];
Edge() {}
Edge(int a, int b, int c) {
to = a, v[0] = b, v[1] = c;
}
};
struct Node{
int v, tot, dist;
Node() {}
Node(int a, int b, int c) : v(a), dist(b), tot(c) {}
bool operator < (const Node &n) const {
if (tot != n.tot) return tot > n.tot;
return dist < n.dist;
}
};
int n, m;
vector<Edge> edges[105];
vector<Edge> redges[105];
int dis[2][105], q[1000000];
bool vis[105];
void spfa_init(int id) {
memset(dis[id], 0x7f, sizeof(dis[id]));
memset(vis, 0, sizeof(vis));
int h = 1, t = 1;
dis[id][n] = 0, vis[n] = 1, q[t++] = n;
while (h < t) {
int cur = q[h++];
for (int i = 0; i < redges[cur].size(); i++) {
Edge e = redges[cur][i];
if (dis[id][cur] + e.v[id] < dis[id][e.to]) {
dis[id][e.to] = dis[id][cur] + e.v[id];
if (!vis[e.to]) vis[e.to] = 1, q[t++] = e.to;
}
}
vis[cur] = 0;
}
}
int ans[2][25];
int spfa_Astar(int id) {
if (dis[id][1] == 0x7f7f7f7f) return -1;
priority_queue<Node> q;
q.push(Node(1, 0, dis[id][1]));
int cnt = 0;
while (!q.empty()) {
Node cur = q.top();
q.pop();
if (cur.v == n) {
ans[id][cnt++] = cur.dist;
if (cnt == 20) return cnt;
}
for (int i = 0; i < edges[cur.v].size(); i++) {
Edge e = edges[cur.v][i];
q.push(Node(e.to, cur.dist + e.v[id], cur.dist + e.v[id] + dis[id][e.to]));
}
}
return cnt;
}
int main() {
n = read();
m = read();
int a, b, c, d;
for (int i = 0; i < m; i++) {
a = read();
b = read();
c = read();
d = read();
edges[a].push_back(Edge(b, c, d));
redges[b].push_back(Edge(a, c, d));
}
spfa_init(0);
spfa_init(1);
int cnt1 = spfa_Astar(0);
int cnt2 = spfa_Astar(1);
if (cnt1 == -1 || cnt2 == -1) {
cout << "IMPOSSIBLE" << endl;
return 0;
}
int res = 0x7f7f7f7f;
bool flag = false;
for (int i = 0; i < cnt1; i++) {
if (flag) break;
for (int j = 0; j < cnt2; j++) {
if (ans[0][i] == ans[1][j]) {
res = min(ans[0][i], res);
flag = true;
break;
}
}
}
if (res == 0x7f7f7f7f) cout << "IMPOSSIBLE" << endl;
else cout << res << endl;
return 0;
}