The Mountain of Gold? UVALive - 6800
2018-04-03 13:16
363 查看
https://cn.vjudge.net/problem/UVALive-6800
题意:
问是否有从0出发并且回到0的负环
假如没有负环每个点最多被松弛n次
所以直接对每条边松弛n次
再看能不能松弛
可以的话就说明存在这样的负环
题意:
问是否有从0出发并且回到0的负环
假如没有负环每个点最多被松弛n次
所以直接对每条边松弛n次
再看能不能松弛
可以的话就说明存在这样的负环
#include <iostream> #include <algorithm> #include <sstream> #include <string> #include <queue> #include <cstdio> #include <map> #include <set> #include <utility> #include <stack> #include <cstring> #include <cmath> #include <vector> #include <ctime> #include <bitset> using namespace std; #define pb push_back #define sd(n) scanf("%d",&n) #define sdd(n,m) scanf("%d%d",&n,&m) #define sddd(n,m,k) scanf("%d%d%d",&n,&m,&k) #define sld(n) scanf("%lld",&n) #define sldd(n,m) scanf("%lld%lld",&n,&m) #define slddd(n,m,k) scanf("%lld%lld%lld",&n,&m,&k) #define sf(n) scanf("%lf",&n) #define sff(n,m) scanf("%lf%lf",&n,&m) #define sfff(n,m,k) scanf("%lf%lf%lf",&n,&m,&k) #define ss(str) scanf("%s",str) #define ansn() printf("%d\n",ans) #define lansn() printf("%lld\n",ans) #define r0(i,n) for(int i=0;i<(n);++i) #define r1(i,e) for(int i=1;i<=e;++i) #define rn(i,e) for(int i=e;i>=1;--i) #define mst(abc,bca) memset(abc,bca,sizeof abc) #define lowbit(a) (a&(-a)) #define all(a) a.begin(),a.end() #define pii pair<int,int> #define pll pair<long long,long long> #define mp(aa,bb) make_pair(aa,bb) #define lrt rt<<1 #define rrt rt<<1|1 #define X first #define Y second #define PI (acos(-1.0)) typedef long long ll; typedef unsigned long long ull; typedef long double ld; const ll mod = 1000000007 ; const double eps=1e-9; const int inf=0x3f3f3f3f; //const ll infl = 100000000000000000;//1e17 const int maxn= 2e3+20; const int maxm = 3e2+20; //muv[i]=(p-(p/i))*muv[p%i]%p; int in(int &ret) { char c; int sgn ; if(c=getchar(),c==EOF)return -1; while(c!='-'&&(c<'0'||c>'9'))c=getchar(); sgn = (c=='-')?-1:1; ret = (c=='-')?0:(c-'0'); while(c=getchar(),c>='0'&&c<='9')ret = ret*10+(c-'0'); ret *=sgn; return 1; } int dp[maxn]; bool vis[maxn]; vector<pii>g[maxn]; int a[maxn],b[maxn],c[maxn]; int dis[maxn]; bool dfs(int u) { if(u==0)return 1; if(vis[u])return dp[u]; vis[u] = 1; int sz = g[u].size(); r0(i,sz) { int v = g[u][i].X; if(dfs(v))return dp[u] = 1; } return dp[u] = 0; } int main() { #ifdef LOCAL freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); #endif // LOCAL int t; sd(t); r1(cas,t) { printf("Case #%d: ",cas); int n,m; sdd(n,m); r0(i,n)g[i].clear(),vis[i] = 0,dis[i] = inf,dp[i] = 0; r1(i,m) { int u,v,w; sddd(u,v,w); a[i] = u,b[i] = v,c[i] = w;; g[u].pb(mp(v,w)); } dis[0] = 0; r0(i,n) { r1(j,m) { int u = a[j],v = b[j],w = c[j]; if(dis[v]>dis[u]+w) { dis[v] = dis[u] + w; } } } bool ok = 0; for(int i=1; i<=m&&!ok; ++i) { int u = a[i],v = b[i]; if(dis[v]>dis[u] + c[i]) { if(dfs(u))ok = 1; } } puts(ok?"possible":"not possible"); } return 0; }
相关文章推荐
- UVAlive 6800 The Mountain of Gold?(负环判断)
- uvalive 6800 - The Mountain of Gold? 判负环
- 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)
- UVAlive 3523 Knights of the Round Table [点双连通分量] [Tarjan]
- uvalive 3523 Knights of the Round Table 圆桌骑士(强连通+二分图)
- UVALive 6342 The Mirror of Galadriel (回文串)
- UVALive3523-Knights of the Round Table(BCC+二分图判定)
- UVALive - 3523 Knights of the Round Table(双联通分量)
- UVALive 3523 Knights of the Round Table(BCC+二分图)
- UVALive 3523 Knights of the Round Table(二分图+双连通分量)
- UVALive 6345 The Glittering Caves of Aglarond (找规律求最多)
- UVAlive3523 Knights of the Round Table(bcc)
- UVALive-3523 Knights of the Round Table (双连通分量+二分图匹配)
- UVALive - 3523 Knights of the Round Table(【点双连通分量】+【二分图判定】)
- UVAlive3523 Knights of the Round Table(bcc)
- UVALive - 7484 Association for the Country of Mububa(dp)
- UVALive-3972 March of the Penguins (最大流:节点容量)
- UVALive - 3972 March of the Penguins(最大流+枚举)