UVAlive 6800 The Mountain of Gold?(负环判断)
来源:互联网 发布:淘宝店铺旺旺在哪里 编辑:程序博客网 时间:2024/06/05 14:50
题目地址:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4812
思路:若存在负环且0点在负环中,则有解。则用SPFA判断是否存在负环,若点i在负环中,则该点的dist置为-INF(下次不再访问),若某点已在负环中,则更新时该点无需进入队列。所以若0点在某个负环中,则dist[0]<0,判断一下即可。
#include<bits/stdc++.h>using namespace std;const int maxn=1000+50;const int INF=0x3f3f3f3f;struct Node{ int v,w; Node(int v=0,int w=0):v(v),w(w) {}};int n,m;int inq[maxn];queue<int> q;int dist[maxn],v[maxn];vector<Node> g[maxn];int solve(int x){ memset(v,0,sizeof(v)); memset(inq,0,sizeof(inq)); while(!q.empty()) q.pop(); for(int i=0; i<=n; i++) dist[i]=INF; dist[x]=0,q.push(x),v[x]=1,inq[x]++; while(!q.empty()) { int now=q.front(); q.pop(),v[now]=0; for(int i=0; i<g[now].size(); i++) { int nt=g[now][i].v; if(dist[nt]>dist[now]+g[now][i].w) { int flag=0; dist[nt]=dist[now]+g[now][i].w; if(!v[nt]&&inq[nt]<=(int)sqrt(n)) { flag=1; v[nt]=1; q.push(nt); inq[nt]++; } if(inq[nt]>(int)sqrt(n)&&flag) dist[nt]=-INF; } } } return dist[0]<0;}int main(){ int t,cas=0; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); for(int i=0;i<=n;i++) g[i].clear(); for(int i=0; i<m; i++) { int x,y,w; scanf("%d%d%d",&x,&y,&w); g[x].push_back(Node(y,w)); } if(solve(0)) printf("Case #%d: possible\n",++cas); else printf("Case #%d: not possible\n",++cas); } return 0;}
0 0
- UVAlive 6800 The Mountain of Gold?(负环判断)
- uvalive 6800 - The Mountain of Gold? 判负环
- UVALive 6800 The Mountain of Gold (bellman_ford判负环)
- UVaLive/LA 6800 The Mountain of Gold?(BellmanFord判负环+记忆化搜索)
- UVaLive/LA 6800 The Mountain of Gold?(BellmanFord判负环+记忆化搜索)
- UVALive - 6800 The Mountain of Gold?(Bellman-ford找负权回路,dfs)
- Wormholes (判断负环)
- UVALive - 4618 Wormholes(负环)
- UVALive - 4618 Wormholes(负环)
- Deliver of the dfo gold
- UVALive 4613 Mountain Road(DP)
- POJ 2983 Is the Information Reliable (差分约束 bell/spfa判断负环)
- UVALive 7661The Size of the Smallest Hole(dfs)
- 偶遇spfa()判断负环
- bzoj 1715(spfa 判断负环)
- easy sssp(spfa判断负环)
- poj 3259 Wormholes(判断负环)
- poj3259Wormholes(bellman_ford判断负环)
- HTML 表单制作
- CSS3 text-overflow 属性
- 数据结构实验之查找五:平方之哈希表
- View的事件体系
- SpringMVC访问静态资源的三种方式
- UVAlive 6800 The Mountain of Gold?(负环判断)
- 面对对象和面向过程
- linux shell awk获得外部变量(变量传值)简介
- MATLAB中对自定义函数进行参数估计的方法
- linux shell awk 流程控制语句(if,for,while,do)详细介绍
- springmvc拦截器学习(一)
- 数据结构实验之排序七:选课名单
- 底部导航库之AHBottomNavigation
- 一个server启两个实例引发的问题