题面:https://www.luogu.org/problem/P3106
首先以n为起点两边spfa,之后再判断所有的边是否在最短路上,以警告次数作为边权再次spfa.
Code:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int N=10005;
struct data{
int u, v;
};
vector<data> f[N], g[N], h[N];
int dis1[N], dis2[N], dis3[N], n, m, vis[N];
void spfa(vector<data> g[N], int d[], int x){
for(int i=1;i<=n;i++){
d[i]=0x3f3f3f3f;
}
memset(vis, 0, sizeof(vis));
vis[x] = 1;
d[x] = 0;
queue<int> q;
q.push(x);
while (!q.empty()){
int x = q.front();
q.pop();
vis[x] = 0;
for (int i = 0; i < g[x].size(); i++){
int u = g[x][i].u, v = g[x][i].v;
if (d[u] > d[x] + v){
d[u] = d[x] + v;
if (!vis[u]){
q.push(u);
vis[u] = 1;
}
}
}
}
}
int main(){
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++){
int a, b, c, d;
scanf("%d%d%d%d", &b, &a, &c, &d);
f[a].push_back({b, c});
g[a].push_back({b, d});
}
spfa(f, dis1, n);
spfa(g, dis2, n);
for (int i = 1; i <= n; i++){
for (int j = 0; j < f[i].size(); j++){
int u = f[i][j].u, p = f[i][j].v, q = g[i][j].v, r = 0;
if (dis1[u] - dis1[i] != p) r++;
if (dis2[u] - dis2[i] != q) r++;
h[u].push_back({i, r});
}
}
spfa(h, dis3, 1);
cout<<dis3[n]<<endl;
return 0;
}
P3106 [USACO14OPEN]GPS的决斗Dueling GPS's
原文地址:https://www.cnblogs.com/ukcxrtjr/p/11711063.html
时间: 2024-10-10 22:30:57