问题 D: Olympic Bus
时间限制: 1 Sec 内存限制: 128 MB
题目描述
There are N cities in JOI Kingdom, numbered from 1 to N. There are M bus lines connecting cities, numbered from 1 to M. The i-th bus line (1 ≤ i ≤ M) runs from the city Ui to the city Vi, and its fare is Ci yen. On the i-th bus line (1 ≤ i ≤ M), a passenger cannot get on the bus in a city other than the city Ui. Also, a passenger cannot get o ff the bus in a city other than the city Vi. There may be more than one bus lines from a city to another city.
The Olympic Games will be held in JOI Kingdom soon. President K is the Minister of Transport of JOI Kingdom. President K will choose at most one bus line, and invert its direction without changing its fare just before the Olympic Games. Namely, if he chooses the i-th bus line (1 ≤ i ≤ M), it will not run from the city Ui to the city Vi during the Olympic Games; instead, it will run from the city Vi to the city Ui. The cost to invert the direction is Di yen, and it will be paid by President K. In order to avoid confusion, it is not allowed to invert the direction during the Olympic Games.
Since President K is the Minister of Transport, during the Olympic Games, he will make a round trip between the city 1 and the city N using the bus lines. By choosing (or not choosing) a bus line to be inverted appropriately,he wants to minimize the sum of the cost of the round trip and the cost to invert the chosen bus line.
Write a program which, given the number of cities and information of the bus lines, calculates the minimum sum of the cost of the round trip and the cost to invert the chosen bus line. If it is not possible to make a round trip between the city 1 and the city N by choosing a bus line to be inverted, output − 1 instead.
输入
Read the following data from the standard input. Given values are all integers.
N M
U1 V1 C1 D1
.
.
.
UM VM CM DM
Constraints
• 2 ≤ N ≤ 200.
• 1 ≤ M ≤ 50 000.
• 1 ≤ Ui ≤ N (1 ≤ i ≤ M).
• 1 ≤ Vi ≤ N (1 ≤ i ≤ M).
• Ui , Vi (1 ≤ i ≤ M).
• 0 ≤ Ci ≤ 1 000 000 (1 ≤ i ≤ M).
• 0 ≤ Di ≤ 1 000 000 000 (1 ≤ i ≤ M).
输出
Write the minimum sum of the cost of the round trip and the cost to invert the chosen bus line to the standard output. If it is not possible to make a round trip between the city 1 and the city N, write − 1 instead.
样例输入
【样例1】
4 5
1 2 4 4
1 3 2 1
4 3 1 2
4 1 6 1
2 4 2 5
【样例2】
4 10
1 2 4 4
1 2 4 4
1 3 2 1
1 3 2 1
4 3 1 2
4 3 1 2
4 1 6 1
4 1 6 1
2 4 2 5
2 4 2 5
【样例3】
4 4
1 2 0 4
1 3 0 1
4 3 0 2
4 1 0 1
【样例4】
4 5
1 2 4 4
1 3 2 4
4 3 1 5
4 1 6 1
2 4 2 5
【样例5】
4 5
2 1 4 4
1 3 2 1
4 3 1 2
4 3 6 1
2 4 2 5
样例输出
【样例1】
10
【样例2】
10
【样例3】
2
【样例4】
12
【样例5】
-1
提示
样例1解释
Assume that President K will invert the direction of the 2nd bus line; its cost is 1 yen. Then, the minimum cost to travel from the city 1 to the city 4 will be 6 yen, and the minimum cost to travel from the city 4 to the city 1 will be 3 yen. Thus, the sum of the cost of the round trip between the city 1 and the city 4 and the cost to invert the chosen bus line will be 10 yen.
Since the sum of the cost of the round trip between the city 1 and the city 4 and the cost to invert the chosen bus line cannot be cheaper than 10 yen, output 10.
样例4解释
It is not necessary to invert the direction of a bus line.
样例5解释
In this sample input, there are 2 bus lines from the city 4 to the city 3.
#pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn= 201;
const int maxm= 50001;
int n, m, u[maxm],v[maxm], c[maxm], d[maxm], vis[maxn];
vector<int> vec[maxn];
int pre[maxn];
bool mrk[maxm];
ll f1[maxn], fn[maxn], g1[maxn], gn[maxn], h1[maxn], hn[maxn], ans;
void dij(ll *d, int s, int fg = 1) {
for (int i = 0; i <= n; ++i) {
d[i] = 1e18;
vis[i] = 0;
}
d[s] = 0;
while (1) {
int u = 0;
for (int j = 1; j <= n; ++j)
if (!vis[j]) {
if (d[j] < d[u]) {
u = j;
}
}
if (!u)
break;
vis[u] = 1;
for (int j : vec[u])
if (d[v[j]] > d[u] + c[j]) {
d[v[j]] = d[u] + c[j];
pre[v[j]] = j;
}
}
if (fg)
for (int i = 1; i <= n; ++i) {
mrk[pre[i]] = 1;
}
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; ++i) {
scanf("%d%d%d%d", &u[i], &v[i], &c[i], &d[i]);
vec[u[i]].push_back(i);
}
dij(f1, 1);
dij(fn, n);
for (int i = 1; i <= n; ++i) {
vec[i].clear();
}
for (int i = 1; i <= m; ++i) {
swap(u[i], v[i]);
vec[u[i]].push_back(i);
}
dij(g1, 1);
dij(gn, n);
for (int i = 1; i <= m; ++i) {
swap(u[i], v[i]);
}
ans = f1[n] + fn[1];
for (int i = 1; i <= m; ++i)
if (mrk[i]) {
swap(u[i], v[i]);
for (int j = 1; j <= n; ++j) vec[j].clear();
for (int j = 1; j <= m; ++j) vec[u[j]].push_back(j);
dij(h1, 1, 0);
dij(hn, n, 0);
ans = min(ans, h1[n] + hn[1] + d[i]);
swap(u[i], v[i]);
} else {
ans = min(ans,
min(f1[n], f1[v[i]] + c[i] + gn[u[i]]) + min(fn[1], fn[v[i]] + c[i] + g1[u[i]]) + d[i]);
}
if (ans >= 1e18) puts("-1"); else printf("%lld
", ans);
}