USACO 2014 US Open, Silver Problem 2. Dueling GPSs

Description

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.

PROBLEM NAME:gpsduel

INPUT FORMAT:

* Line 1: The integers N and M.

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

SAMPLE INPUT (file gpsduel.in):

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

INPUT DETAILS:

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.

OUTPUT FORMAT:

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

SAMPLE OUTPUT (file gpsduel.out):

1

OUTPUT DETAILS:

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.



题目大意(来自洛谷,侵权就让Dreif翻译咯):

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

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

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

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

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

只需注意一点:当走的边不是当前点到n的最短路径上的边时受到警告;

所以显然: 反向建边 + SPFA*3;

代码:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<queue>
 5 using namespace std;
 6
 7 const int maxn = 10000 + 5;
 8 const int maxm = 50000 + 5;
 9 struct Edge
10 {
11     int f, t, v[3];
12 }gra[maxm];
13 int n, m;
14 int fir[maxn], nxt[maxm], dis[maxn], path[maxn];
15 bool used[maxn];
16 queue<int>Q;
17 void SPFA(int);
18
19 int main()
20 {
21     freopen("gpsduel.in", "r", stdin);
22     freopen("gpsduel.out", "w", stdout);
23     memset(fir, -1, sizeof(fir));
24     scanf("%d%d", &n, &m);
25     for(int i = 1; i <= m; i++)
26     {
27         Edge &tmp = gra[i];
28         scanf("%d%d%d%d", &tmp.t, &tmp.f, &tmp.v[0], &tmp.v[1]);
29         tmp.v[2] = 2;
30         nxt[i] = fir[tmp.f], fir[tmp.f] = i;
31     }
32     SPFA(0);
33     SPFA(1);
34     SPFA(2);
35     printf("%d\n", dis[1]);
36     return 0;
37 }
38
39 void SPFA(int j)
40 {
41     memset(dis, 0x7f, sizeof(dis));
42     memset(path, 0, sizeof(path));
43     dis[n] = 0;
44     Q.push(n);
45     while(!Q.empty())
46     {
47         int k = Q.front();
48         Q.pop();
49         used[k] = 0;
50         for(int i = fir[k]; ~i; i = nxt[i])
51         {
52             int tmp = gra[i].t;
53             if(dis[tmp] > dis[k] + gra[i].v[j])
54             {
55                 dis[tmp] = dis[k] + gra[i].v[j];
56                 path[tmp] = i;
57                 if(used[tmp]) continue;
58                 Q.push(tmp);
59                 used[tmp] = 1;
60             }
61         }
62     }
63     for(int i = 1; i <= n; i++)
64         gra[path[i]].v[2]--;
65 }
时间: 2024-10-30 00:00:42

USACO 2014 US Open, Silver Problem 2. Dueling GPSs的相关文章

USACO 2007 December Contest, Silver Problem 2. Building Roads Kruskal最小生成树算法

PROBLEM: (ENGLISH VERSION) Farmer John had just acquired several new farms! He wants to connect the farms with roads so that he can travel from any farm to any other farm via a sequence of roads; roads already connect some of the farms. Each of the N

USACO翻译:USACO 2014 DEC Silver三题

USACO 2014 DEC SILVER 一.题目概览 中文题目名称 回程 奶牛IDs 搬家 英文题目名称 piggyback cowids relocate 可执行文件名 piggyback cowids relocate 输入文件名 piggyback.in cowids.in relocate.in 输出文件名 piggyback.out cowids.out relocate.out 每个测试点时限 1秒 1秒 1秒 测试点数目 10 10 10 每个测试点分值 10 10 10 比较

USACO翻译:USACO 2014 MARCH Silver三题

USACO 2014 MARCH 一.题目概览 中文题目名称 农田灌溉 懒牛 牛叫 英文题目名称 irrigation lazy mooomoo 可执行文件名 irrigation lazy mooomoo 输入文件名 irrigation.in lazy.in mooomoo.in 输出文件名 irrigation.out lazy.out mooomoo.out 每个测试点时限 1秒 1秒 1秒 测试点数目 10 10 10 每个测试点分值 10 10 10 比较方式 全文比较 全文比较 全

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跑最

USACO翻译:USACO 2014 JAN三题(2)

USACO 2014 JAN 一.题目概览 中文题目名称 队伍平衡 滑雪录像 滑雪场建设 英文题目名称 bteams recording skicourse 可执行文件名 bteams recording skicourse 输入文件名 bteams.in recording.in skicourse.in 输出文件名 bteams.out recording.out skicourse.out 每个测试点时限 1秒 1秒 1秒 测试点数目 10 10 10 每个测试点分值 10 10 10 比

USACO翻译:USACO 2014 US Open 三题

USACO 2014 US Open 一.题目概览 中文题目名称 牧场装饰 里程表 牛像展览 英文题目名称 decorate odometer fairphoto 可执行文件名 decorate odometer fairphoto 输入文件名 decorate.in odometer.in fairphoto.in 输出文件名 decorate.out odometer.out fairphoto.out 每个测试点时限 1秒 1秒 1秒 测试点数目 10 10 10 每个测试点分值 10 1

POJ 2230 Watchcow &amp;&amp; USACO Watchcow 2005 January Silver (欧拉回路)

题意: Bessie 最近做了农场看守,他每天晚上的工作就是巡视农场并且保证没有坏人破坏农场.从谷仓出发去巡视,并且最终回到谷仓. Bessie 视力不是很好,不能像其他农场的看守一样,对农场的每一条连接不同场地的路走一遍就可以发现是不是有异常情况,他需要每条路都走两遍,并且这两边必须是不同的方向,因为他觉得自己应该不会两次都忽略农场中的异常情况. 每块地之间一定会由至少一条路相连.现在的任务就是帮他制定巡视路线.前提假设一定存在满足题意的路径. 输入: 第一行输入两个数N(2 <= N <=

Above the Median&amp;Dueling GPSs

2557: Above the Median 时间限制: 1 Sec  内存限制: 64 MB提交: 14  解决: 9[提交] [状态] [命题人:admin] 题目描述 Farmer John has lined up his N (1 <= N <= 100,000) cows in a row to measure their heights; cow i has height H_i (1 <= H_i <= 1,000,000,000) nanometers--FJ b

USACO翻译:USACO 2014 NOV Silver三题

USACO 2013 NOV SILVER 一.题目概览 中文题目名称 未有的奶牛 农场航线 贝西洗牌 英文题目名称 nocow vacation shuffle 可执行文件名 nocow vacation shuffle 输入文件名 nocow.in vacation.in shuffle.in 输出文件名 nocow.out vacation.out shuffle.out 每个测试点时限 1秒 1秒 1秒 测试点数目 10 10 10 每个测试点分值 10 10 10 比较方式 全文比较