Amount The key is to read the question. I didn't know what it meant until I read the title report anyway. Give you n points. M route. Two-way. Time consuming. W Worm hole. One-way. The time is negative. Ask if you can return from a certain point of view before you see yourself. That is to judge whether there is negative ring. Using the Bellman_ford algorithm.
Minutes to finish. A long line of bugs. Or the loop there J and I silly points unclear.
Attached code:
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#define MAXN 0x1f1f1f1f
using namespace Std;
int N, M, W; Fixed number m+w is the number of sides
struct Edge
{
int u, V, t;
} edge[6000];
int low[600];
int tot;
BOOL Bellman_ford ()
{
memset (Low, MAXN, sizeof (MAXN));
for (int i=2; i<=n; ++i)
Low[i] = MAXN;
LOW[1] = 0;
for (int i=1; i<n; i++)
{
BOOL flag = FALSE;
for (int j=0; j<tot; ++j)
{
if (LOW[EDGE[J].V] > low[edge[j].u] + edge[j].t)
{
LOW[EDGE[J].V] = low[edge[j].u] + edge[j].t;
Flag = true;
}
}
if (!flag) break;
}
for (int i=0; i<tot; ++i)
{
if (LOW[EDGE[I].V] > low[edge[i].u] + edge[i].t)
return true; There is a negative ring
}
return false;
}
int main ()
{
int t;
scanf ("%d", &t);
while (t--)
{
scanf ("%d%d%d", &n, &m, &w);
tot = 0;
int u, V, t;
for (int i=0; i<m; ++i)
{
scanf ("%d%d%d", &u, &v, &t);
edge[tot].u = u;
EDGE[TOT].V = v;
edge[tot++].t = t;
edge[tot].u = v;
EDGE[TOT].V = u;
edge[tot++].t = t;
}
for (int i=0; i<w; ++i)
{
scanf ("%d%d%d", &u, &v, &t);
edge[tot].u = u;
EDGE[TOT].V = v;
edge[tot++].t =-T;
}
BOOL flag = Bellman_ford ();
if (flag) printf ("yes\n");
else printf ("no\n");
}
return 0;
}
POJ 3259 Bellman_ford algorithm