poj 3259 Wormholes(spfa)
2015-06-15 20:41
417 查看
[code]#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 ; int n,m,w,cnt ,vis ,dist ; 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; }
相关文章推荐
- Appium移动自动化测试(四)--one demo
- poj 3259 Wormholes(spfa)
- (笔试题)程序运行时间
- 关于ffmpeg封装h264为mp4的问题
- (int)a、&a、(int)&a、(int&)a的区别
- git学习笔记
- Hive导入10G数据的测试
- 数据清洗小记:判断是否为日期函数
- 制作站点计数监听器
- 数字在排序数组中出现的次数
- 广州本田整车销售系统技术分析(五)
- Oracle R12采购接收流程(PR-PO-RCV-AP-Payment)
- c多文件编译
- 学习RecyclerView的一些东西
- 【性能诊断】六、并发场景的性能分析(windbg案例,大量的内部异常造成CPU飙升)
- aam中的训练过程
- iOS动画:UIView动画和CALayer动画(CABasicAnimation、CAKeyframeAnimation的使用)
- oracle中rowid的用法 (全面)
- hadoop学习笔记:hadoop文件系统浅析
- “重力锁屏”软件简介