UVA11090-Going in Cycle!!-最短路/图判环/二分
来源:互联网 发布:淘宝号心怎么升级最快 编辑:程序博客网 时间:2024/06/05 17:28
UVA11090-Going in Cycle!!-最短路
Description
You are given a weighted directed graph with n vertices and m edges. Each cycle in the graph has a
weight, which equals to sum of its edges. There are so many cycles in the graph with different weights.
In this problem we want to find a cycle with the minimum mean.
Input
The first line of input gives the number of cases, N. N test cases follow. Each one starts with two
numbers n and m. m lines follow, each has three positive number a, b, c which means there is an edge
from vertex a to b with weight of c.
O#utput
For each test case output one line containing Case #x: followed by a number that is the lowest mean
cycle in graph with 2 digits after decimal place, if there is a cycle. Otherwise print No cycle found..
Constraints
- n ≤ 50
- a, b ≤ n
- c ≤ 10000000
Sample Input
22 11 2 12 21 2 22 1 3
Sample Output
Case #1: No cycle found.Case #2: 2.50
思路
存在一个边数为a的大小为m的圈 –> 边和-am<=0 –> 每个边都-m,判断有没有负环
AC代码
/************************************** *Source : UVA11090 *Knowledge Point : SSSP *Author : CSUzick**************************************/#include <cstdio>#include <cstdlib>#include <iomanip>#include <cstring>#include <iostream>#include <stack>#include <vector>#include <queue>#include <cctype>#include <cmath>#include <string>#include <algorithm>#include <set>#include <bitset>#include <map>#define INF 0x3f3f3f3f3f3fLL#define ULL unsigned long long#define LL long long#define N 100100#define eps 10e-9#define MOD 100000000#define mem(a,n) memset(a,n,sizeof(a))#define fread freopen("in.txt","r",stdin)#define fwrite freopen("out.txt","w",stdout)using namespace std;const double PI=acos(-1.0);struct Edge{ int from,to,dist; Edge(int u,int v,int d):from(u),to(v),dist(d){};};struct SPFA{ vector<Edge> edges; vector<int> G[N]; bool inque[N]; double d[N]; int cnt[N]; int n,m; 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=edges.size(); G[from].push_back(m-1); } bool spfa(double mid){ queue<int> que; memset(inque,0,sizeof(inque)); memset(cnt,0,sizeof(cnt)); for(int i=1;i<=n;++i){ que.push(i); d[i]=0; } while(!que.empty()){ int temp=que.front(); if(cnt[temp]==n){ return true; } que.pop(); inque[temp]=false; for(int i=0;i<G[temp].size();++i){ Edge &e=edges[G[temp][i]]; if(d[e.to]>d[temp]+e.dist-mid){ d[e.to]=d[temp]+e.dist-mid; if(!inque[e.to]){ que.push(e.to); inque[e.to]=true; if(++cnt[e.to]>=n) return true; } } } } return false; }};SPFA SMF;int main(){ int t,kase=0,n,m,u,c,v; double maxl,high,low,mid; scanf("%d",&t); while(kase<t){ maxl=0; scanf("%d%d",&n,&m); SMF.init(n); while(m--){ scanf("%d%d%d",&u,&v,&c); maxl=max(maxl,(double)c); SMF.AddEdge(u,v,c); } high=maxl+1,low=0; if(!SMF.spfa(high)){ printf("Case #%d: No cycle found.\n",++kase); continue; } while(high-low>eps){ mid=(low+high)/2; if(SMF.spfa(mid)){ high=mid; }else{ low=mid; } } printf("Case #%d: %.2f\n",++kase,mid); } return 0;}
- UVA11090-Going in Cycle!!-最短路/图判环/二分
- uva11090 Going in Cycle!! --- 二分+spfa判负环
- uva11090 Going in Cycle!!
- uva11090 - Going in Cycle!!
- UVa11090 Going in Cycle!!
- 【UVA11090】Going in Cycle!!
- uva11090【Going in Cycle】
- UVA11090 Going in Cycle!! 解题报告【判负环】【SPFA】【二分答案】
- UVA11090 [Going in Cycle!!] 二分答案+SPFA判负圈
- Uva 11090 Going in Cycle!!(二分+最短路)
- UVA11090 Going in Cycle!! (二分+SPFA判断有无负权)
- uva11090 Going in Cycle!!题解(负环)
- UVA 11090 - Going in Cycle!!(最短路`Bellman-Ford)
- 最短路判负环uva11090
- UVA 11090 Going in Cycle!!(二分 + spfa)
- UVA 11090 Going in Cycle!(BellmanFord+二分)
- Uva 11090 - Going in Cycle!! bellman-ford 负权环 二分
- UVA 11090 Going in Cycle!!(二分 + spfa 判负环)
- bzoj3211 花神游历各国(线段树)
- 一道动态规划的poj
- int main(int argc, char* argv[])
- 百度之星 2017初赛第一场 1001 小C的倍数问题
- 2017年8月12日训练日记
- UVA11090-Going in Cycle!!-最短路/图判环/二分
- hdu 6113 度度熊的01世界/2017 百度之星
- python数据结构之字典
- iWebSMS2000短信发送问题记录
- 为什么工厂模式是华而不实的—浅谈工厂模式的利与弊
- web.xml 中元素加载顺序及其详解
- Java Web开发环境搭建
- Java中switch理解
- N的阶乘 mod M(同余)