poj 3259 Wormholes(spfa)
来源:互联网 发布:html5 java 编辑:程序博客网 时间:2024/05/16 14:13
#include<stdio.h>#include<queue>#include<string.h>#include<algorithm>using namespace std;const int inf=0x3f3f3f3f;const int N=1024;struct node{ int to; int w; node *next;};node* edge[N];int n,m,w,cnt[N],vis[N],dist[N];queue<int>q;int spfa(int v0){ while(!q.empty()) q.pop(); int i,u; node *ptr; for(i=1; i<N; i++) { dist[i]=inf; vis[i]=0; cnt[i]=0; } dist[v0]=0; vis[v0]=1; q.push(v0); cnt[v0]++; while(!q.empty()) { u=q.front(); q.pop(); vis[u]=0; if(cnt[u]>n) return 1; ptr=edge[u]; while(ptr!=NULL) { if(dist[ptr->to]>dist[u]+ptr->w) { dist[ptr->to]=dist[u]+ptr->w; if(vis[ptr->to]==0) { vis[ptr->to]=1; cnt[ptr->to]++; q.push(ptr->to); } } ptr=ptr->next; } } return 0;}int main(){ int _,i,u,v,t,flag; node* temp; scanf("%d",&_); while(_--) { scanf("%d%d%d",&n,&m,&w); for(i=1; i<N; i++) edge[i]=NULL; for(i=0; i<m; i++) { scanf("%d%d%d",&u,&v,&t); temp=new node; temp->to=v; temp->w=t; temp->next=NULL; if(edge[u]==NULL) { edge[u]=temp; } else { temp->next=edge[u]; edge[u]=temp; } temp=new node; temp->to=u; temp->w=t; temp->next=NULL; if(edge[v]==NULL) { edge[v]=temp; } else { temp->next=edge[v]; edge[v]=temp; } } for(i=0; i<w; i++) { scanf("%d%d%d",&u,&v,&t); temp=new node; temp->to=v; temp->w=-t; temp->next=NULL; if(edge[u]==NULL) { edge[u]=temp; } else { temp->next=edge[u]; edge[u]=temp; } } flag=spfa(1); if(flag) printf("YES\n"); else printf("NO\n"); } return 0;}
0 0
- poj 3259 Wormholes //SPFA
- poj 3259 Wormholes SPFA
- POJ 3259 Wormholes(SPFA)
- Wormholes - POJ 3259 spfa
- poj 3259 Wormholes(spfa)
- POJ 3259:Wormholes 【SPFA】
- Wormholes POJ 3259【SPFA】
- poj 3259-- Wormholes(SPFA)
- POJ-3259 Wormholes(SPFA)
- Poj 3259 Wormholes ( SPFA
- POJ 3259 Wormholes spfa
- [POJ 3259] Wormholes [SPFA]
- poj 3259 Wormholes spfa算法
- 【POJ 3259】Wormholes 【spfa判负环】
- poj 3259--Wormholes 【spfa判负环】
- POJ 3259-Wormholes-SPFA判负环
- POJ 3259 Wormholes SPFA判负环
- poj 3259 Wormholes【spfa判负环】
- 关于ffmpeg封装h264为mp4的问题
- git学习笔记
- 二叉树的深度
- 忍——致命的缺点
- js+php实现实时网页聊天功能
- poj 3259 Wormholes(spfa)
- Linux性能测试:gprof篇
- MJRefresh刷新第三方库
- 黑马程序员——Java基础---数组
- iOS UIView 切其中某一个圆角
- Java web分层设计
- C语言中的常用变量
- MySQL常用操作
- 入门程序员在开发过程中遇到的困难