【UVA11090】Going in Cycle!!
来源:互联网 发布:软件测评收费 编辑:程序博客网 时间:2024/06/15 17:30
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 Format
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.
- Output Format
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.
- Sample Input
2
2 1
1 2 1
2 2
1 2 2
2 1 3
- Sample Output
Case #1: No cycle found.
Case #2: 2.50
- Hint
n ≤ 50
a, b ≤ n
c ≤ 10000000
- 题目大意
给定一个有n个顶点m条边的加权有向图,如果图中存在环(回路),环的平均值等于,环上边的权值之和除以构成环的边数,图中可能不止存在一个回路,计算平均权值最小的回路。
- 分析
这道题刚开始打的时候想法是从每个没被访问过点开始进行深搜,若搜到的点在本次搜索时已经被搜到过了,则认为发现一个环。每次搜索过的点都视为被访问过。但后来有神犇跟我说这样做不仅会超时还会少找到几个环(我也不知到为什么)。
正解:
这是一道最优比例环,属于01分数规划,即求一个环上某些信息的比值最优为多少。
显然答案满足单调性质,所以可以二分答案。对于一个二分出的Lim若有
实际上就是把找出的环上的边权减去Lim,如果是个负环,则Lim合法。至于找负环用Spfa即可,如果一个点被入队超过n次,则这个点于一个负环上。
#include <queue>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;queue <int> Q;const int N=55;const double Inf=1e15,Eps=1e-8;int last[N],Count[N],In[N],T,n,m,u,v,tot,Case;double Dist[N],L,R,Mid,w;struct Data{ int to,next; double val;}E[N*N];void Addline(int u,int v,double w){ E[++tot].to=v; E[tot].next=last[u]; E[tot].val=w; last[u]=tot;}bool Check(double Lim){ for (int i=1;i<=n;i++) Count[i]=0,Dist[i]=Inf,In[i]=0; for (;!Q.empty();) Q.pop(); for (int i=1;i<=n;i++) if (!Count[i]) for (Q.push(i),In[i]=1,Count[i]=1,Dist[i]=0;!Q.empty();){ int u=Q.front();In[Q.front()]=0,Q.pop(); for (int j=last[u];j;j=E[j].next){ int v=E[j].to; if (Dist[v]>Dist[u]+E[j].val-Lim){ Dist[v]=Dist[u]+E[j].val-Lim; Count[v]=Count[u]+1; if (Count[v]>n) return true; if (In[v]) continue; In[v]=1; Q.push(v); } } } return false;}int main(){ //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); for (scanf("%d",&T);T;T--){ memset(last,0,sizeof(last)); tot=0; Case++; scanf("%d%d",&n,&m); for (int i=1;i<=m;i++){ scanf("%d%d %lf",&u,&v,&w); Addline(u,v,w); } for (L=0,R=Inf,Mid=(L+R)*0.5;R-L>=Eps;Mid=(L+R)*0.5){ if (Check(Mid)) R=Mid; else L=Mid; } if (R==Inf) printf("Case #%d: No cycle found.\n",Case); else printf("Case #%d: %.2f\n",Case,R); } //fclose(stdin); fclose(stdout); return 0;}
- 易错点
这题的数据可能存在自环,所以Spfa的时候取出队首后应马上把队首在队中的标记取消(据说模板就应该是这样的),否则会出现一些玄学的问题。今晚一整晚都在调这个问题,最后还是hjj大神把他的程序一点一点的改成我的样式,最后才找出来。我实在是太菜了!O_O
- 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!!题解(负环)
- UVA11090 Going in Cycle!! 解题报告【判负环】【SPFA】【二分答案】
- UVA11090-Going in Cycle!!-最短路/图判环/二分
- UVA11090 [Going in Cycle!!] 二分答案+SPFA判负圈
- UVA11090 Going in Cycle!! (二分+SPFA判断有无负权)
- Going in Cycle!! uva
- Uva 11090 Going in Cycle!!
- Uva-11090-Going in Cycle!!
- UVA 11090 - Going in Cycle!!
- uva 11090 Going in Cycle!!
- UVA - 11090-Going in Cycle!!
- UVA 11090 Going in Cycle!!
- NOSQL数据模型
- 安卓的Flag
- hdu 5317 (普通素数筛的应用)+(前缀和预处理)
- canvas学习心得之arcTo
- 嵌入式学习
- 【UVA11090】Going in Cycle!!
- 今天如此之忙
- css选择器
- 错误的安全配置
- swift_003(Swift的?和!)
- Python字符串之格式规约
- 机器学习—梯度下降算法
- CNN数值初始化——xavier(上)
- Thinkphp 逻辑与,逻辑或的复合查询