o-extended traffic (judging negative ring)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.