3538: [Usaco2014 open]dueling GPS time limit: 1 sec Memory limit: 128 MB
S ubmit: 162 solved: 93
[submit][status][discuss] Description
Farmer John had recently purchased a new car online, but in his haste he accidentally clicked the "Submit" button twice wh En selecting extra features for the car, and as a result of the car ended up equipped with both GPS navigation systems! Even worse, the both systems often make conflicting decisions on the route that FJ should take. The map of the region in which FJ lives consists of N intersections (2 <= N <=) and M directional roads (1 &l t;= 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 and separate directional roads in opposite orientations. FJ ' s house was located at intersection 1, and the his farm was located at intersection N. It is possible to reach the farm from the He house by traveling along a series of directional roads. Both GPS units is using the same underlying map as DescriBed above; However, they has 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 a integer in the range 1..100,000). FJ wants to the farm. However, each GPS unit complains loudly any time FJ follows a road (say, from intersection X to intersection Y) that the G PS unit believes not to being part of a shortest route from X to the farm (it's even possible that both GPS units can Compla In, if FJ takes a road that neither unit likes). FJ determine the minimum possible number of total complaints he can receive if he chooses his route appropriat Ely. If both GPS units complain when FJ follows a road, this counts as +2 towards the total.
Give you a map of n points, there may be heavy edges. There are two GPS positioning systems, which are considered to be pi, and QI, through the time of the edge I. On each side of the line, if a system thinks that this side of the walk is not the shortest that it thinks, it will be warned once T two systems are separate warnings, that is, when the side of the walk is not in the two systems considered the shortest range, it will be 2 times warning. Ask for a solution, 1àn, at least how many times you need to be warned. Input
* Line 1:the integers N and M. Line I describes road I with four integers:a_i b_i p_i q_i. Output
* Line 1:the Minimum total number of complaints FJ can receive if he routes himself from his house to the farm optimally.
Sample Input 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 is 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 for time to traverse, and the second GPS thinks it takes 1 unit of time, etc. Sample Output 1
OUTPUT details:if FJ follows 1, 2, 4, 5, then the first GPS complains on the 1 road (it w Ould prefer the 1-3 road instead). However, for the rest of the Route 2, 4--5, both GPSs is happy, since-a shortest route from 2 to 5 Acco Rding to each GPS.
Exercises
Two times reverse spfa+ a forward SPFA
WA took two times, SPFA did not modify vis when out of the team. As a dead show.
Code:
/************************************************************** problem:3538 user:wjyi language:c++ Res ult:accepted time:148 Ms memory:2808 KB ****************************************************************/#incl ude<iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm > #include <cmath> #include <queue> #define MAXN 10005 #define MAXM 50005 #define INF 1<<29 using names
Pace std;
int read () {int X=0,f=1;char ch=getchar (); while (ch< ' 0 ' | |
Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();}
while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();}
return x*f;
} int tot,head[maxn],n,d[3][maxn],m, U[MAXM],V[MAXM],P[MAXM],Q[MAXM];
BOOL VIS[MAXN]; struct node{int to,next,w;}
E[MAXM];
void Add (int u,int v) {e[++tot]= (Node) {v,head[u]};head[u]=tot;} queue<int> Q;
void Spfa (int s,int k) {for (int i=1;i<=n;i++) D[k][i]=inf; memset (vis,0,sizeof (vis));
Vis[s]=1;d[k][s]=0;q.push (s);
while (!q.empty ()) {int Now=q.front (); Q.pop (); for (int i=head[now];i;i=e[i].next) if (D[K][E[I].TO]>D[K][NOW]+E[I].W) {d[k][e[i].to
]=D[K][NOW]+E[I].W;
if (!vis[e[i].to]) {vis[e[i].to]=1;
Q.push (e[i].to);
}} vis[now]=0;
}} int main () {n=read (); M=read (); int u,v;
memset (head,0,sizeof (head));
for (int i=1;i<=m;i++) {u=read (); V=read (); Add (V,u); U[i]=u;
V[i]=v; P[i]=read ();
Q[i]=read ();
} for (int i=1;i<=tot;i++) e[i].w=p[i];
SPFA (n,0);
for (int i=1;i<=tot;i++) e[i].w=q[i];
SPFA (n,1);
memset (head,0,sizeof (head)); tot=0;
for (int i=1;i<=m;i++) {u=u[i];v=v[i];
Add (u,v); e[i].w=2;
if (D[0][u]==d[0][v]+p[i])--E[I].W;
if (D[1][u]==d[1][v]+q[i])--E[I].W; } SPFA ();
if (D[2][n]==inf) d[2][n]=-1;
printf ("%d\n", D[2][n]);
return 0; }