題目描述
給定n個點,m條無向邊,初始狀態下手裏有s個銀幣。從u點到v點需要花費a個硬幣,b個時間單位。在每個點可以花d個時間單位兌換c個銀幣,求從起點1到各個點需要的最短時間。
思路
這題很關鍵的一個突破口是數據範圍:50個點,從u到v花費不超過50銀幣,所以總花費不超過2500.通過一個二維數組dis[ i ][ j ]來表示到達 i 點時還剩下 j 個銀幣時需要的時間最小值,然後跑一遍最短路,最後遍歷一遍就可以輸出最小值。
注意銀幣最大只需要2500,所以輸入s的時候記得判斷
s = min(s, 2500);
代碼
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const LL inf = 0x3f3f3f3f3f3f3f3f;
const int N = 55, M = 110;
struct Edge {
int to, next;
LL a, b;
}e[M * 2];
int head[N], idx;
LL c[N], d[N];
LL dis[N][2510];
int n, m, s;
void add(int u, int v, LL a, LL b) {
e[++idx].to = v;
e[idx].next = head[u];
e[idx].a = a;
e[idx].b = b;
head[u] = idx;
}
void spfa() {
queue<PII> q;
for(int i = 1; i <= n; i++) {
for(int j = 0; j <= 2500; j++) {
dis[i][j] = inf;
}
}
dis[1][s] = 0;
q.push({1, s});
while(!q.empty()) {
int u = q.front().first, w = q.front().second; q.pop();
for(int i = head[u]; i; i = e[i].next) {
int v = e[i].to;
LL a = e[i].a, b = e[i].b;
if(w >= a && dis[v][w - a] > dis[u][w] + b) {
dis[v][w - a] = dis[u][w] + b;
q.push({v, w - a});
}
}
if(dis[u][min(w + c[u], (LL)2500)] > dis[u][w] + d[u]) {
dis[u][min(w + c[u], (LL)2500)] = dis[u][w] + d[u];
q.push({u, min(w + c[u], (LL)2500)});
}
}
}
int main() {
scanf("%d%d%d", &n, &m, &s);
s = min(s, 2500);
while(m--) {
int u, v;
LL a, b;
scanf("%d%d%lld%lld", &u, &v, &a, &b);
add(u, v, a, b);
add(v, u, a, b);
}
for(int i = 1; i <= n; i++) {
scanf("%lld %lld", &c[i], &d[i]);
}
spfa();
for(int i = 2; i <= n; i++) {
LL res = inf;
for(int j = 0; j <= 2500; j++) res = min(res, dis[i][j]);
printf("%lld\n", res);
}
return 0;
}