| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
g++ spfa 试着判了有没有联通什么的 留下代码做纪念//poj3259
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;
const int MAXN=501;
const int inf=0x3ffffff;
vector<int>G[MAXN];
int n,m,w;
queue <int >que;
int times[MAXN];
int d[MAXN];
int cost[MAXN][MAXN];
bool find_negative_loop(int start){
if(times[start]!=0)return false;
memset(times,0,sizeof(times));
que.push(start);
d[start]=0;
times[start]=1;
while(!que.empty()){
int from=que.front();que.pop();
int len=G[from].size();
for(int i=0;i<len;i++){
int to=G[from][i];
if(d[to]>d[from]+cost[from][to]){
d[to]=d[from]+cost[from][to];
que.push(to);
times[to]++;
if(times[to]>=n)return true;
}
}
}
return false;
}
int main(){
int f;
scanf("%d",&f);
while(f--){
scanf("%d %d %d",&n,&m,&w);
for(int i=0;i<=n;i++)fill(cost[i],cost[i]+n+1,inf);
memset(times,0,sizeof(times));
for(int i=0;i<m;i++){
int from,to,tcost;
scanf("%d %d %d",&from,&to,&tcost);
G[from].push_back(to);
cost[from][to]=min(cost[from][to],tcost);
G[to].push_back(from);
cost[to][from]=min(cost[to][from],tcost);
}
for(int i=0;i<w;i++){
int from,to,tcost;
scanf("%d %d %d",&from,&to,&tcost);
G[from].push_back(to);
cost[from][to]=min(cost[from][to],-tcost);
}
bool fl=false;
for(int i=1;i<=n;i++){
fill(d,d+n+1,inf);
fl=find_negative_loop(i);
if(fl)break;
}
if(fl)printf("YES\n");
else printf("NO\n");
}
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator