POJ 2983 Is the Information Reliable?(差分约束系统+BellmanFord)
来源:互联网 发布:最新网络捕鱼平台 编辑:程序博客网 时间:2024/06/05 18:31
=号转化为>=,<=。
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<queue>using namespace std;const int maxn=1000+5;struct edge{ int from,to,dist;};struct BellmanFord{ int n,m; vector<edge> edges; vector<int> G[maxn]; bool inq[maxn]; int d[maxn]; int cnt[maxn]; void init(int n){ this->n=n; for(int i=0;i<n;i++) G[i].clear(); edges.clear(); } void addedge(int from,int to,int dist){ edges.push_back((edge){from,to,dist}); m=(int)edges.size(); G[from].push_back(m-1); } bool negativeCycle(){ queue<int> q; memset(inq,0,sizeof(inq)); memset(cnt,0,sizeof(cnt)); for(int i=0;i<n;i++) {d[i]=0;inq[0]=true;q.push(i);} while(!q.empty()){ int u=q.front();q.pop(); inq[u]=false; for(int i=0;i<G[u].size();i++){ edge &e=edges[G[u][i]]; if(d[e.to]>d[u]+e.dist){ d[e.to]=d[u]+e.dist; if(!inq[e.to]) {inq[e.to]=true;q.push(e.to);if(++cnt[e.to]>n) return true;} } } } return false; }};BellmanFord solver;int n,m;int main(){ while(~scanf("%d%d",&n,&m)) { solver.init(n); char ch[3]; int u,v,d; for(int i=0;i<m;i++){ scanf("%s",ch); if(ch[0]=='P'){ scanf("%d%d%d",&u,&v,&d); u--;v--; solver.addedge(v,u,d); solver.addedge(u,v,-d); } else{ scanf("%d%d",&u,&v); u--;v--; solver.addedge(u,v,-1); } } if(solver.negativeCycle()) printf("Unreliable\n"); else printf("Reliable\n"); } return 0;}
0 0
- POJ 2983 Is the Information Reliable?(差分约束系统+BellmanFord)
- POJ 2983 Is the Information Reliable(差分约束系统)
- POJ 2983 Is the Information Reliable?(差分约束系统)
- POJ 2983-Is the Information Reliable?(差分约束系统)
- POJ 2983 Is the Information Reliable? 差分约束系统
- PoJ 2983 Is the Information Reliable? 差分约束系统
- POJ--2983[Is the Information Reliable?] 差分约束
- poj 2983 差分约束Is the Information Reliable?
- poj 2983 Is the Information Reliable?(差分约束)
- POJ 2983 Is the Information Reliable? 依旧差分约束
- poj 2983 Is the Information Reliable? 差分约束
- POJ-2983-Is the Information Reliable?(差分约束)
- poj 2983 Is the Information Reliable? 差分约束
- POJ 2983 Is the Information Reliable? 差分约束
- poj-2983 Is the Information Reliable? 差分约束+判负环
- poj 2983 Is the Information Reliable?(差分约束)
- [POJ 2983]Is the Information Reliable?[差分约束]
- POJ 2983Is the Information Reliable? (差分约束)
- hdu1864(多重背包)
- 郝斌的C语言基础 108 如何定义函数
- Mac版R语言入门(二)R语言的基本命令操作
- 今天配置apche导致无法连接服务器
- 点击整行选定checkbox
- POJ 2983 Is the Information Reliable?(差分约束系统+BellmanFord)
- 让easyui-datebox控件只显示年份和月份
- 定时器
- 上传图片本地html+css
- hadoop源码之hdfs(1)---通信协议
- Github上优秀的开源项目
- 【每日一课】5、Dos窗口及常用命令
- Ubuntu下搭建LAMP
- 怎么判断32位系统下是大端还是小端