Test instructions: There are n cities, each city has a congestion degree aI, from one city I to another city J time is: (Aj-ai) ^3, existence negative ring. Q. When is the time to reach the K-City from the first city, if it cannot be reached, or if the time is less than 3 output? Otherwise, the output takes time.
This problem has a negative ring, and therefore requires all negative rings (including the position of the negative ring can be reached), can be used in the number of queues to judge
#include <stdio.h>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <string.h>
#include <math.h>
usingnamespaceStd
ConstintMAXN =100005;
ConstintMAXM =205;
Const intOO =0xFFFFFFF;
structNode
{
intU, V, C, next;
}E[MAXN];
intDIS[MAXM], HEAD[MAXM];
intBUSY[MAXN], P[MAXN];//p Array Records total number of accesses
BOOLUSE[MAXM];
voidADD (intUintVintCintK
{
e[k].u = u;
E[K].V = v;
E[K].C = C*c*c;
E[k].next = Head[u];
Head[u] = k;
}
voidSPFA (intN
{
queue<int> Q;
Q.push (1);
while(Q.size ())
{
inti = Q.front (); Q.pop ();
Use[i] =false, p[i]++;
for(intJ=head[i]; j!=0; J=e[j].next)
{
intU = e[j].u, v = e[j].v, c = e[j].c;
if(Dis[u]+c < dis[v])
{
DIS[V] = dis[u]+c;
if(Use[v] = =false&& P[v] <= N)
{
USE[V] =true;
Q.push (v);
}
}
}
}
}
intMain ()
{
intT, t=1;
scanf"%d", &t);
while(t--)
{
intI, N, M, k=1, u, v;
scanf"%d", &n);
for(i=1; i<=n; i++)
{
Dis[i] = oo;
Head[i] =0;
P[i] =0;
scanf"%d", &busy[i]);
}
scanf"%d", &m);
while(m--)
{
scanf"%d%d", &u, &v);
ADD (U, V, busy[v]-busy[u], k++);
}
scanf"%d", &m);
printf"Case %d:\n", t++);
dis[1] =0;
SPFA (N);
while(m--)
{
scanf"%d", &v);
if(Dis[v] = = OO | | dis[v] <3|| P[V] > N)
printf"? \ n");
Else
printf"%d\n", Dis[v]);
}
}
return0;
}
o-extended traffic (judging negative ring)