bellman-ford算法判断环。
//Memory Time
//1260K 360MS //不添加delete
//488K 985MS //添加delete之后
#include<iostream>
#include<queue>
using namespace std;
struct edge{
edge* next;
int dst;
int cost;
};
struct vextex{
int cost;
int deep;
bool isAddInQueue;
};
vextex vex[1000];
edge* eg[1000];
int main()
{
int N;
cin >> N;
for(int j = 0; j < N; j++)
{
int vNum, posCost, negCost;
cin >> vNum >> posCost >> negCost;
for(int i = 1; i <= vNum; i++)
{
eg[i] = NULL;
vex[i].cost = 2000000000;
vex[i].deep = 0;
vex[i].isAddInQueue = false;
}
for(int i = 0; i < posCost; i++)
{
int src,dst,cost;
cin>>src>>dst>>cost;
edge* e = new edge;
e->cost = cost;
e->dst = dst;
e->next = eg[src];
eg[src] = e;
e = new edge;
e->cost = cost;
e->dst = src;
e->next = eg[dst];
eg[dst] = e;
}
for(int i = 0; i < negCost; i++)
{
int src,dst,cost;
cin>>src>>dst>>cost;
edge* e = new edge;
e->cost = -cost;
e->dst = dst;
e->next = eg[src];
eg[src] = e;
}
vex[1].cost = 0;
vex[1].deep = 1;
vex[1].isAddInQueue = true;
int maxDeep = 0;
queue<int> que;
que.push(1);
while(!que.empty())
{
int tempSrc = que.front();
vex[tempSrc].isAddInQueue = false;
que.pop();
edge* e = eg[tempSrc];
while(e != NULL)
{
int tempDst = e->dst;
int tempCost = vex[tempSrc].cost + e->cost;
if(tempCost < vex[tempDst].cost)
{
vex[tempDst].cost = tempCost;
vex[tempDst].deep = vex[tempSrc].deep+1;
if(vex[tempDst].deep > maxDeep)
maxDeep = vex[tempDst].deep;
if(vex[tempDst].isAddInQueue == false)
{
vex[tempDst].isAddInQueue = true;
que.push(tempDst);
}
}
e = e->next;
}
if(maxDeep > vNum)
break;
}
// for(int i = 1; i <= vNum; i++)
// {
// edge* e = eg[i];
// edge*tempE;
// while(e != NULL)
// {
// tempE = e;
// e = e->next;
// delete tempE;
// }
// }
if(maxDeep > vNum)
cout << "YES" <<endl;
else
cout << "NO" <<endl;
}
return 0;
}