今天的每日一题确实有点坑,可能评测姬炸了,不过又学到最短路了
推理过程:
- 这个题有点小思维,其实也没啥,就是从起始点到每个点的距离,用白天的费用算一遍,然后用终止点到每个点的距离的费用,用黑夜的费用算一遍
- 然后由于有个限制条件,所以我们找最小值的时候应该从后往前找。
- 交了差不多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;
}