[USACO14OPEN] Dueling GPS's[最短路建模]

题目描述

Farmer John has recently purchased a new car online, but in his haste he accidentally clicked the "Submit" button twice when selecting extra features for the car, and as a result the car ended up equipped with two GPS navigation systems! Even worse, the two systems often make conflicting decisions about the route that FJ should take.

The map of the region in which FJ lives consists of N intersections (2 <= N <= 10,000) and M directional roads (1 <= M <= 50,000). Road i connects intersections A_i (1 <= A_i <= N) and B_i (1 <= B_i <= N). Multiple roads could connect the same pair of intersections, and a bi-directional road (one permitting two-way travel) is represented by two separate directional roads in opposite orientations. FJ‘s house is located at intersection 1, and his farm is located at intersection N. It is possible to reach the farm from his house by traveling along a series of directional roads.

Both GPS units are using the same underlying map as described above; however, they have different notions for the travel time along each road. Road i takes P_i units of time to traverse according to the first GPS unit, and Q_i units of time to traverse according to the second unit (each travel time is an integer in the range 1..100,000).

FJ wants to travel from his house to the farm. However, each GPS unit complains loudly any time FJ follows a road (say, from intersection X to intersection Y) that the GPS unit believes not to be part of a shortest route from X to the farm (it is even possible that both GPS units can complain, if FJ takes a road that neither unit likes).

Please help FJ determine the minimum possible number of total complaints he can receive if he chooses his route appropriately. If both GPS units complain when FJ follows a road, this counts as +2 towards the total.

给你一个N个点的有向图,可能有重边.

有两个GPS定位系统,分别认为经过边i的时间为Pi,和Qi.

每走一条边的时候,如果一个系统认为走的这条边不是它认为的最短路,就会受到警告一次T T

两个系统是分开警告的,就是说当走的这条边都不在两个系统认为的最短路范围内,就会受到2次警告.

如果边(u,v)不在u到n的最短路径上,这条边就受到一次警告,求从1到n最少受到多少次警告。

输入输出格式

输入格式:

  • Line 1: The integers N and M.

Line i describes road i with four integers: A_i B_i P_i Q_i.

输出格式:

  • Line 1: The minimum total number of complaints FJ can receive if he routes himself from his house to the farm optimally.

输入输出样例

输入样例#1:

5 7
3 4 7 1
1 3 2 20
1 4 17 18
4 5 25 3
1 2 10 1
3 5 4 14
2 4 6 5

输出样例#1:

1

说明

There are 5 intersections and 7 directional roads. The first road connects from intersection 3 to intersection 4; the first GPS thinks this road takes 7 units of time to traverse, and the second GPS thinks it takes 1 unit of time, etc.

If FJ follows the path 1 -> 2 -> 4 -> 5, then the first GPS complains on the 1 -> 2 road (it would prefer the 1 -> 3 road instead). However, for the rest of the route 2 -> 4 -> 5, both GPSs are happy, since this is a shortest route from 2 to 5 according to each GPS.



感觉好神

先求两次最短路,然后新建图只要不在一条最短路上就边w++,再求最短路

注意GPS认为的最短路是到达n,所以前两次反向建图

PS:沙茶的数组M写成N

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int N=1e4+5,M=5e4+5,INF=1e9+5;
typedef long long ll;
inline int read(){
    char c=getchar();int x=0,f=1;
    while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1;c=getchar();}
    while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘;c=getchar();}
    return x*f;
}
int n,m,u[M],v[M],w1[M],w2[M];
struct edge{
    int u,v,ne,w1,w2;
}e[M];
struct edge2{
    int v,ne,w;
}en[M];
int h[N],cnt=0;
inline void ins(int u,int v,int w1,int w2){
    cnt++;
    e[cnt].u=h[u];
    e[cnt].v=v;e[cnt].ne=h[u];h[u]=cnt;e[cnt].w1=w1;e[cnt].w2=w2;
}
inline void add(int u,int v,int w){
    cnt++;
    en[cnt].v=v;en[cnt].w=w;en[cnt].ne=h[u];h[u]=cnt;
}
int q[N],head=0,tail=0,inq[N],d1[N],d2[N],d3[N];
inline void lop(int &x){if(x==N-1) x=1;}
void spfa1(int d[]){
    for(int i=1;i<=n;i++) d[i]=INF;
    memset(inq,0,sizeof(inq));
    head=tail=1;
    q[tail++]=n;inq[n]=1;d[n]=0;
    while(head!=tail){
        int u=q[head++];inq[u]=0;lop(head);
        for(int i=h[u];i;i=e[i].ne){
            int v=e[i].v,w=e[i].w1;
            if(d[v]>d[u]+w){
                d[v]=d[u]+w;
                if(!inq[v]){q[tail++]=v;inq[v]=1;lop(tail);}
            }
        }
    }
}
void spfa2(int d[]){
    for(int i=1;i<=n;i++) d[i]=INF;
    memset(inq,0,sizeof(inq));
    head=tail=1;
    q[tail++]=n;inq[n]=1;d[n]=0;
    while(head!=tail){
        int u=q[head++];inq[u]=0;lop(head);
        for(int i=h[u];i;i=e[i].ne){
            int v=e[i].v,w=e[i].w2;
            if(d[v]>d[u]+w){
                d[v]=d[u]+w;
                if(!inq[v]){q[tail++]=v;inq[v]=1;lop(tail);}
            }
        }
    }
}

void buildGraph(){
    cnt=0;
    memset(h,0,sizeof(h));
    for(int i=1;i<=m;i++){
        int w=0;
        if(d1[u[i]]<d1[v[i]]+w1[i]) w++;
        if(d2[u[i]]<d2[v[i]]+w2[i]) w++;
        add(u[i],v[i],w);//printf("add %d %d %d\n",u,v,w);
    }
}
void spfa3(int d[]){
    for(int i=1;i<=n;i++) d[i]=INF;
    memset(inq,0,sizeof(inq));
    head=tail=1;
    q[tail++]=1;inq[1]=1;d[1]=0;
    while(head!=tail){
        int u=q[head++];inq[u]=0;lop(head);//printf("spfa3 %d\n",u);
        for(int i=h[u];i;i=en[i].ne){
            int v=en[i].v,w=en[i].w;
            if(d[v]>d[u]+w){
                d[v]=d[u]+w;
                if(!inq[v]){q[tail++]=v;inq[v]=1;lop(tail);}
            }
        }
    }
}
int main(){
    n=read();m=read();
    for(int i=1;i<=m;i++){
        u[i]=read();v[i]=read();w1[i]=read();w2[i]=read();
        ins(v[i],u[i],w1[i],w2[i]);
    }
    spfa1(d1);
    spfa2(d2);
    buildGraph();
    spfa3(d3);
    printf("%d",d3[n]);
}

[USACO14OPEN] Dueling GPS's[最短路建模]

时间: 2024-12-18 19:29:53

[USACO14OPEN] Dueling GPS's[最短路建模]的相关文章

洛谷 P3106 [USACO14OPEN]GPS的决斗Dueling GPS&#39;s

P3106 [USACO14OPEN]GPS的决斗Dueling GPS's 题目描述 Farmer John has recently purchased a new car online, but in his haste he accidentally clicked the "Submit" button twice when selecting extra features for the car, and as a result the car ended up equip

[USACO14OPEN]GPS的决斗Dueling GPS&#39;s

题目概况 题目描述 给你一个\(N\)个点的有向图,可能有重边. 有两个\(GPS\)定位系统,分别认为经过边\(i\)的时间为\(P_i\),和\(Q_i\). 每走一条边的时候,如果一个系统认为走的这条边不是它认为的最短路,就会受到警告一次. 两个系统是分开警告的,就是说当走的这条边都不在两个系统认为的最短路范围内,就会受到2次警告. 如果边\((u,v)\)不在\(u\)到\(n\)的最短路径上,这条边就受到一次警告,求从\(1\)到\(n\)最少受到多少次警告. 输入格式 第一行,两个整

P3106 [USACO14OPEN]GPS的决斗Dueling GPS&#39;s

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

BZOJ3538: [Usaco2014 Open]Dueling GPS

3538: [Usaco2014 Open]Dueling GPS Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 59  Solved: 36[Submit][Status] Description Farmer John has recently purchased a new car online, but in his haste he accidentally clicked the "Submit" button twice w

USACO 2014 US Open Dueling GPS&#39;s /// SPFA

题目大意: 给定n个点m条边的有向图 有两个GPS 分别认为 A[i]到B[i] 的一条边的花费是P[i].Q[i] 当当前走的边不是GPS认为的最短路上的边就会被警告 即两个GPS都不认为是最短路上的边时 会被警告两次 求从点1走到点n被警告次数最少是多少次 https://blog.csdn.net/oakley_/article/details/52510465 按P[i]反向建图 再从n跑最短路到1 然后遍历所有的边判断将不是最短路的边C[i]+1 Q[i]也同样 最后按C[i]从1跑最

P3106 GPS的决斗Dueling GPS&#39;s

目录 题目 思路 Code 同步:https://buringstraw.win/index.php/archives/43/ 题目 Farmer John has recently purchased a new car online, but in his haste he accidentally clicked the "Submit" button twice when selecting extra features for the car, and as a result

【BZOJ2407/4398】探险/福慧双修 最短路建模

[BZOJ2407]探险 Description 探险家小T好高兴!X国要举办一次溶洞探险比赛,获奖者将得到丰厚奖品哦!小T虽然对奖品不感兴趣,但是这个大振名声的机会当然不能错过! 比赛即将开始,工作人员说明了这次比赛的规则:每个溶洞和其他某些溶洞有暗道相连.两个溶洞之间可能有多条道路,也有可能没有,但没有一条暗道直接从自己连到自己.参赛者需要统一从一个大溶洞出发,并再次回到这个大溶洞. 如果就这么点限制,那么问题就太简单了,可是举办方又提出了一个条件:不能经过同一条暗道两次.这个条件让大家犯难

BZOJ2118墨墨的等式[数论 最短路建模]

2118: 墨墨的等式 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 1317  Solved: 504[Submit][Status][Discuss] Description 墨墨突然对等式很感兴趣,他正在研究a1x1+a2y2+…+anxn=B存在非负整数解的条件,他要求你编写一个程序,给定N.{an}.以及B的取值范围,求出有多少B可以使等式存在非负整数解. Input 输入的第一行包含3个正整数,分别表示N.BMin.BMax分别表示数

POJ1062昂贵的聘礼[最短路建模]

昂贵的聘礼 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 45892   Accepted: 13614 Description 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金币作为聘礼才答应把女儿嫁给他.探险家拿不出这么多金币,便请求酋长降低要求.酋长说:"嗯,如果你能够替我弄到大祭司的皮袄,我可以只要8000金币.如果你能够弄来他的水晶球,那么只要5000