码迷,mamicode.com
首页 > 其他好文 > 详细

P3106 [USACO14OPEN]GPS的决斗Dueling GPS's

时间:2019-10-21 00:11:00      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:pop   cout   queue   pre   cstring   str   algo   stream   name   

题面: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

标签:pop   cout   queue   pre   cstring   str   algo   stream   name   

原文地址:https://www.cnblogs.com/ukcxrtjr/p/11711063.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!