今天的每日一題確實有點坑,可能評測姬炸了,不過又學到最短路了
推理過程:
- 這個題有點小思維,其實也沒啥,就是從起始點到每個點的距離,用白天的費用算一遍,然後用終止點到每個點的距離的費用,用黑夜的費用算一遍
- 然後由於有個限制條件,所以我們找最小值的時候應該從後往前找。
- 交了差不多30發,不過學到了unordered_map 以後查詢就用他了
代碼:
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <map>
#include <unordered_map>
using namespace std;
typedef pair<int,int> PII;
const int N = 100010, M = 200010;
int n ,m,idx;
int e[M],ne[M],h[N];
long long dist1[M], dist2[M], res[N];
bool st[N];
unordered_map<int,map<int,long long> >mp1, mp2;
void add(int x,int y){
e[idx] = y, ne[idx] = h[x],h[x] = idx++;
}
void spfa1(int u){
memset(dist1,0x3f,sizeof dist1);
dist1[u] = 0;
queue <int> q;
q.push(u);
st[u] = true;
while(q.size()){
int t = q.front();
q.pop();
st[t] = false;
for (int i = h[t]; i != -1; i = ne[i]){
int j = e[i];
if (dist1[j] > dist1[t] + mp1[t][j]){
dist1[j] = dist1[t] + mp1[t][j];
if (!st[j]){
q.push(j);
st[j] = true;
}
}
}
}
}
void spfa2(int u){
memset(dist2,0x3f,sizeof dist2);
dist2[u] = 0;
queue <int> q;
q.push(u);
st[u] = true;
while(q.size()){
int t = q.front();
q.pop();
st[t] = false;
for (int i = h[t]; i != -1; i = ne[i]){
int j = e[i];
if (dist2[j] > dist2[t] + mp2[t][j]){
dist2[j] = dist2[t] + mp2[t][j];
if (!st[j]){
q.push(j);
st[j] = true;
}
}
}
}
}
int main(){
scanf("%d%d",&n,&m);
memset(h,-1,sizeof h);
for (int i = 0; i < m; i ++){
int x , y , a, b;
scanf("%d%d%d%d",&x,&y,&a,&b);
add(x,y);
add(y,x);
mp1[x][y] = a;
mp1[y][x] = a;
mp2[x][y] = b;
mp2[y][x] = b;
}
int t, s;
scanf("%d%d",&t,&s);
spfa1(t);
memset(st,false,sizeof st);
spfa2(s);
long long mx = 1e18;
for (int i = n; i >= 1; i --){
mx = min(mx,dist1[i] + dist2[i]);
res[i] = mx;
}
for (int i = 1; i <= n; i ++){
printf("%lld\n",res[i]);
}
return 0;
}