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

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

时间:2017-02-22 21:11:18      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:警告   live   submit   getch   john   mini   题意   esc   direct   

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

标签:警告   live   submit   getch   john   mini   题意   esc   direct   

原文地址:http://www.cnblogs.com/suishiguang/p/6430850.html

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