洛谷 P3106 [USACO14OPEN]GPS的决斗Dueling GPS'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 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.

啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊!!!!!!!!!!!!!!

挺了四天的题目终于过了~~~~~~~~~~~~

题意:

给你一个N个点的有向图,设定初始位置为1,结束位置为n。有两个GPS定位系统,分别认为经过边i的时间为Pi,和Qi.每走一条边的时候,如果一个系统认为走的这条边不是它认为的最短路,就会受到警告一次。如果走的这条边都不在两个系统认为的最短路范围内,就会受到2次警告。求最少需要受到多少次警告。n≤10000,边数≤50000

  1 #include<iostream>
  2 #include<cstring>
  3 #include<queue>
  4 #include<cstdio>
  5 using namespace std;
  6 #define maxn 100010
  7 #define MAXN 100010
  8 #define mse(a,x) memset(a,x,sizeof(a));
  9 queue<int>q;
 10 int n,m,tot,ans,u[MAXN],v[MAXN],value1[MAXN];
 11 int value2[MAXN],dis_1[10001],dis_2[10001];
 12 int dis[maxn],head[maxn],link[maxn];
 13 struct Edge{
 14     int to,next,w_one,w_two;
 15 }e[maxn],ee[maxn];
 16 inline int read(){
 17     int x=0,f=1;
 18     char ch=getchar();
 19     while(ch<‘0‘ || ch >‘9‘){
 20         if (ch == ‘-‘)f=-1;
 21         ch=getchar();
 22     }
 23     while(ch>=‘0‘&&ch<=‘9‘){
 24         x=x*10+ch-‘0‘;
 25         ch=getchar();
 26     }
 27     return x*f;
 28 }
 29 void SPFA_First(){
 30     bool exist[maxn];
 31     while(!q.empty())q.pop();
 32     mse(dis_1,0x3f);mse(exist,false);
 33     dis_1[n]=0;q.push(n);exist[n]=true;
 34     while(!q.empty()){
 35         int p=q.front();q.pop();exist[p]=false;
 36         for(int i=head[p];i;i=e[i].next){
 37             int v=e[i].to;
 38             if(dis_1[v]>dis_1[p]+e[i].w_one){
 39                 dis_1[v]=dis_1[p]+e[i].w_one;
 40                 if(!exist[v]){
 41                     q.push(v);exist[v]=true;
 42                 }
 43             }
 44         }
 45     }
 46 }
 47 void SPFA_Second(){
 48     bool exist[maxn];
 49     while(!q.empty())q.pop();
 50     mse(dis_2,0x3f);mse(exist,false);
 51     dis_2[n]=0;q.push(n);exist[n]=true;
 52     while(!q.empty()){
 53         int p=q.front();q.pop();exist[p]=false;
 54         for(int i=head[p];i;i=e[i].next){
 55             int v=e[i].to;
 56             if(dis_2[v]>dis_2[p]+e[i].w_two){
 57                 dis_2[v]=dis_2[p]+e[i].w_two;
 58                 if(!exist[v]){
 59                     q.push(v);exist[v]=true;
 60                 }
 61             }
 62         }
 63     }
 64 }
 65
 66 void Add_Edge(int u,int v,int w1,int w2){
 67     e[++tot].to=v;e[tot].w_one=w1;e[tot].w_two=w2;
 68     e[tot].next=head[u];head[u]=tot;
 69 }
 70 void SPFA_Third(){
 71     bool exist[maxn];
 72     while(!q.empty())q.pop();
 73     mse(exist,false);mse(dis,0x3f);
 74     dis[1]=0;exist[1]=1;q.push(1);
 75     while(!q.empty()){
 76         int now=q.front();q.pop();exist[now]=false;
 77         for (int i=link[now];i;i=ee[i].next) {
 78             if (dis[now]+ee[i].w_one<dis[ee[i].to]){
 79                 dis[ee[i].to]=dis[now]+ee[i].w_one;
 80                 if (!exist[ee[i].to]) {
 81                     q.push(ee[i].to);
 82                     exist[e[i].to]=1;
 83                 }
 84             }
 85         }
 86     }
 87 }
 88 int main()
 89 {
 90     n=read();m=read();
 91     for(int i=1;i<=m;i++){
 92         u[i]=read();v[i]=read();
 93         value1[i]=read();value2[i]=read();
 94         Add_Edge(v[i],u[i],value1[i],value2[i]);
 95         // 注意这里是 建的 反边
 96     }
 97     SPFA_First();
 98     SPFA_Second();
 99     for(int i=1;i<=m;i++){
100         ee[i].to=v[i];ee[i].next=link[u[i]];
101         link[u[i]]=i;
102         if(dis_1[v[i]]+value1[i]>dis_1[u[i]])
103             ee[i].w_one++;
104         if(dis_2[v[i]]+value2[i]>dis_2[u[i]])
105             ee[i].w_one++;
106     }
107     SPFA_Third();
108     printf("%d",dis[n]);
109     return 0;
110 }

终于可以骄傲的删掉那篇 待解决

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

时间: 2024-10-11 06:26:14

洛谷 P3106 [USACO14OPEN]GPS的决斗Dueling GPS's的相关文章

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;

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

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

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

[USACO14OPEN] Dueling GPS&#39;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! Ev

洛谷 P2299 Mzc和体委的争夺战

洛谷 P2299 Mzc和体委的争夺战 题目背景 mzc与djn第四弹. 题目描述 mzc家很有钱(开玩笑),他家有n个男家丁(做过前三弹的都知道).但如此之多的男家丁吸引来了我们的体委(矮胖小伙),他要来与mzc争夺男家丁. mzc很生气,决定与其决斗,但cat的体力确实有些不稳定,所以他需要你来帮他计算一下最短需要的时间. 输入输出格式 输入格式: 第一行有两个数n,m.n表示有n个停留站,m表示共有m条路. 之后m行,每行三个数a_i \; b_i \; c_iai?bi?ci? ,表示第

洛谷 P2709 BZOJ 3781 小B的询问

题目描述 小B有一个序列,包含N个1~K之间的整数.他一共有M个询问,每个询问给定一个区间[L..R],求Sigma(c(i)^2)的值,其中i的值从1到K,其中c(i)表示数字i在[L..R]中的重复次数.小B请你帮助他回答询问. 输入输出格式 输入格式: 第一行,三个整数N.M.K. 第二行,N个整数,表示小B的序列. 接下来的M行,每行两个整数L.R. 输出格式: M行,每行一个整数,其中第i行的整数表示第i个询问的答案. 输入输出样例 输入样例#1: 6 4 3 1 3 2 1 1 3

洛谷1231 教辅的组成

洛谷1231 教辅的组成 https://www.luogu.org/problem/show?pid=1231 题目背景 滚粗了的HansBug在收拾旧语文书,然而他发现了什么奇妙的东西. 题目描述 蒟蒻HansBug在一本语文书里面发现了一本答案,然而他却明明记得这书应该还包含一份练习题.然而出现在他眼前的书多得数不胜数,其中有书,有答案,有练习册.已知一个完整的书册均应该包含且仅包含一本书.一本练习册和一份答案,然而现在全都乱做了一团.许多书上面的字迹都已经模糊了,然而HansBug还是可

洛谷教主花园dp

洛谷-教主的花园-动态规划 题目描述 教主有着一个环形的花园,他想在花园周围均匀地种上n棵树,但是教主花园的土壤很特别,每个位置适合种的树都不一样,一些树可能会因为不适合这个位置的土壤而损失观赏价值. 教主最喜欢3种树,这3种树的高度分别为10,20,30.教主希望这一圈树种得有层次感,所以任何一个位置的树要比它相邻的两棵树的高度都高或者都低,并且在此条件下,教主想要你设计出一套方案,使得观赏价值之和最高. 输入输出格式 输入格式: 输入文件garden.in的第1行为一个正整数n,表示需要种的

洛谷 P2801 教主的魔法 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:https://www.luogu.org/problem/show?pid=2801 题目描述 教主最近学会了一种神奇的魔法,能够使人长高.于是他准备演示给XMYZ信息组每个英雄看.于是N个英雄们又一次聚集在了一起,这次他们排成了一列,被编号为1.2.…….N. 每个人的身高一开始都是不超过1000的正整数.教主的魔法每次可以把闭区间[L, R](1≤L≤R≤N)内的英雄的身高全部加上一个整数W.(虽然L=R时并不