poj 3259 Wormholes (BELLman—FOrd算法)(邻接矩阵表示)
2015-06-15 20:17
253 查看
http://poj.org/problem?id=3259
之前一开始 ,没做出来,搁置了好几天才看见bug所在。所以今天a掉了 ,把代码贴出来,这个使用邻接矩阵表示的 ,下一篇用邻接表可以提高效率。
之前一开始 ,没做出来,搁置了好几天才看见bug所在。所以今天a掉了 ,把代码贴出来,这个使用邻接矩阵表示的 ,下一篇用邻接表可以提高效率。
#include<iostream> #include<queue> #include<stdio.h> #include<string.h> using namespace std; const int INF=600; int G[INF][INF]; int inq[INF]; int res[INF]; int dict[INF]; int n,m,w; bool spfa(int x) { queue<int>cnt; memset(dict,0,sizeof(dict)); cnt.push(x); memset(res,0x1f,sizeof(res)); memset(inq,0,sizeof(inq)); res[x]=0; dict[x]++; while(!cnt.empty()) { int cur=cnt.front(); cnt.pop(); inq[cur]=0; for(int i=1; i<=n; i++) { if(res[i]>res[cur]+G[cur][i]) { res[i]=res[cur]+G[cur][i]; if(!inq[i]) { cnt.push(i); inq[i]=1; if(++dict[i]>=n) return 1; } } } } return 0; } int main() { int s,e,t; int T; cin>>T; while(T--) { cin>>n>>m>>w; memset(G,0x1f,sizeof(G)); int k; for(int i=0; i<m; i++) { cin>>s>>e>>t; G[s][e]=G[e][s]= G[e][s]>t? t:G[s][e]; k=s; } for(int j=0; j<w; j++) { cin>>s>>e>>t; G[s][e]=G[s][e]>-t?-t : G[s][e]; } printf("%s\n",spfa(1)?"YES":"NO"); } return 0; }
相关文章推荐
- leetcode 25:Reverse Nodes in k-Group
- css兼容问题
- unity3D 音频播放
- OWIN轻量型框架介绍
- 沉淀生命,沉淀自己
- Happy Problem【leetcode202】
- JAVA中FutureTask
- Ubuntu安装Oracle管理软件sqldeveloper--最方便方式
- 注册 Jdbc 驱动程序的三种方式及Class.forName 的作用
- 从上往下打印二叉树
- 一些免费的WebService的服务网站
- Reveal的使用
- Macbook的android studio创建project时,加载gradle慢的解决办法
- CentOS安装cmake
- mac 命令行su使用
- Java for LeetCode 225 Implement Stack using Queues
- Android数据库更新并保留原来数据的实现
- 黑马程序员--结构体知识总结
- android开机动画切换
- poj1258