题意杀....
题意:给n+w个顶点和m条边,边的信息为时间,其中w表示虫洞,边的时间取负数代表可以回到过去,问时间能不能回到过去
题解:给出的n个顶点和边是双向的,而虫洞是单向并且边权值取负值,建图然后bellman-ford判负环就好了
<span style="font-size:14px;">#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
using namespace std;
#define maxn 505
#define LL long long
int cas=1,T;
int n,m,w;
const int INF = 1<<29;
struct Edge
{
int from,to,dist;
Edge(int u,int v,int d):from(u),to(v),dist(d){}
};
struct Bellman_ford
{
vector<Edge>edges;
vector<int> G[maxn];
int d[maxn];
int inq[maxn];
int cnt[maxn];
void init()
{
for (int i = 0;i<=n;i++)
G[i].clear();
edges.clear();
}
void AddEdge(int from,int to,int dist)
{
edges.push_back(Edge(from,to,dist));
int mm = edges.size();
G[from].push_back(mm-1);
}
bool bellman_ford(int s)
{
queue<int>q;
memset(inq,0,sizeof(inq));
memset(cnt,0,sizeof(cnt));
for (int i =0;i<=n;i++)
d[i]=INF;
d[s]=0;
inq[s]=1;
q.push(s);
while (!q.empty())
{
int u = q.front();q.pop();
inq[u] = false;
for (int i =0;i<G[u].size();i++)
{
Edge &e = edges[G[u][i]];
if (d[u]<INF && d[e.to]>d[u]+e.dist)
{
d[e.to] = d[u] + e.dist;
if (!inq[e.to])
{
q.push(e.to);
inq[e.to]=1;
if (++cnt[e.to]>n)
return false;
}
}
}
}
return true;
}
};
int main()
{
//freopen("in","r",stdin);
scanf("%d",&T);
while (T--)
{
scanf("%d%d%d",&n,&m,&w);
Bellman_ford bf;
bf.init();
for (int i = 0;i<m;i++)
{
int u,v,t;
scanf("%d%d%d",&u,&v,&t);
bf.AddEdge(u,v,t);
bf.AddEdge(v,u,t);
}
for (int i = 0;i<w;i++)
{
int u,v,t;
scanf("%d%d%d",&u,&v,&t);
bf.AddEdge(u,v,-t);
}
if (!bf.bellman_ford(1))
puts("YES");
else
puts("NO");
}
//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
return 0;
}</span>
题目