UVa 515 King (差分约束+线性规划+判负圈)
2015-08-06 21:42
513 查看
#include<cstdio> #include<algorithm> #include<cstring> #include<cmath> #include<iostream> #include<queue> #include<vector> #include<set> #include<climits> #include<map> #include<string> using namespace std; const int INF=100000000; const int maxn=100+10; const int maxm=100*100+10; int n,m; int first[maxn]; int u[maxm],v[maxm],nnext[maxm],w[maxm]; int V,E; int d[maxn]; bool Bellman_Ford() { queue<int> q; bool inq[maxn]; int c[maxn]; V=0; for(int i=0;i<=n+1;i++)d[i]=(i==n+1 ? 0:INF); memset(inq,0,sizeof(inq)); memset(c,0,sizeof(c)); q.push(n+1); while(!q.empty()) { int x=q.front(); q.pop(); inq[x]=false; for(int e=first[x];e!=-1;e=nnext[e])if(d[v[e]]>d[x]+w[e]) { d[v[e]]=d[x]+w[e]; if(!inq[v[e]]) { inq[v[e]]=true; q.push(v[e]); c[v[e]]++; if(c[v[e]]==n+2)return true; } } } return false; } int main() { while(~scanf("%d",&n)) { if(n==0)break; scanf("%d",&m); for(int i=0;i<=n+1;i++) first[i]=-1; E=0; for(int i=0;i<m;i++) { char op[10]; int si,ni,ki; scanf("%d %d %s %d",&si,&ni,op,&ki); if(op[0]=='g') { u[E]=si+ni; v[E]=si-1; w[E]=-ki-1; nnext[E]=first[u[E]]; first[u[E]]=E; E++; } else { u[E]=si-1; v[E]=si+ni; w[E]=ki-1; nnext[E]=first[u[E]]; first[u[E]]=E; E++; } } for(int i=0;i<n+1;i++) { u[E]=n+1; v[E]=i; w[E]=0; nnext[E]=first[u[E]]; first[u[E]]=E; E++; } if(Bellman_Ford())printf("successful conspiracy\n"); else printf("lamentable kingdom\n"); } return 0; }
相关文章推荐
- 使用NFS文件系统与嵌入式设备传输文件
- Linux 1
- [CODEVS1220]数字三角形
- USACO OPEN 12 BOOKSELF(转)
- 约瑟夫问题(c++实现)
- echo server
- hdoj 1242 Rescue
- [CODEVS1294]全排列
- java--网络编程--黑马程序员
- poj2109Power of Cryptography 大数开根
- 对一个程序的思考5(指针与所指数据类型的关系)
- HDU 1728 逃离迷宫 (搜索)
- Android应用性能优化之使用SQLiteStatement优化SQLite操作
- 深度学习(四)卷积神经网络入门学习
- [洛谷1157]组合的输出
- java--多线程(2)--黑马程序员
- 理解Java的IO流 2015年8月6日 21:30:38
- mysql 高可用 2
- 如何使用Createjs来编写HTML5游戏(四)TweenJS和Tick动画
- UE4 符号调试器