您的位置:首页 > 编程语言 > Go语言

UVA - 11090-Going in Cycle!!

2014-08-21 23:02 267 查看
E - Going in Cycle!!

Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu

Submit Status

Description

Download as PDF

I I U P C 2 0 06

Problem G: Going in Cycle!!

Input: standard input

Output: standard output

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.

Output

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

Output for Sample Input

2

2 1

1 2 1

2 2

1 2 2

2 1 3

Case #1: No cycle found.

Case #2: 2.50

Problemsetter: Mohammad Tavakoli Ghinani

Alternate Solution: Cho

题目大意:问你是否能在一个单向路里面找到一个环使这个环的权值除以这个构成这个环边数的值最小。

思路:二分枚举这个平均值,将每条边都减去这个平均值,然后用spfa判断是否存在负环,如果存在负环说明这个平均值大了 将l = mid 如果不存在负环 说明平均值小了 将r = mid一直二分100次左右可以使mid接近最终答案误差在10的-30次方以内。

代码:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <queue>
#include <cstdlib>
#include <cstring>
#include <algorithm>
//typedef __int64 LL;
using namespace std;
int vis[110],head[110],cnt = 0,n,m,count1[110];
double d[110];
struct Edge
{
int u,v,next;
double w;
Edge(int a= 0, int b =0, double c =0)
{
u = a; v = b; w = c;
}
}e[10000];
void add(Edge q)
{
e[cnt].u = q.u;
e[cnt].v = q.v;
e[cnt].w = q.w;
e[cnt].next = head[q.u];
head[q.u] = cnt++;
}
bool spfa(double mid)
{
memset(vis,0,sizeof(vis));
memset(count1,0,sizeof(count1));
int i;
memset(d,0,sizeof(d));
int q[100],tot = 0;
for(i = 1; i <= n; i++)
{
q[tot++] = i;
vis[i] = 1;
}
while (tot)
{
int w = q[--tot];
vis[w] = 0;
for(i = head[w]; i != -1; i = e[i].next)
{
int end = e[i].v;
if(d[end] > d[w] +e[i].w  -mid)
{
d[end] = d[w] +e[i].w-mid;
if(vis[end] == 0)
{
vis[end] = 1;
q[tot++] = end;
count1[end] ++;
if(count1[end] > n)
return true;
}
}
}
}
return false;
}
bool ok(double mid)
{
int i,j;
bool ans;
if(spfa(mid)) ans =1;
else ans =0;
return ans;
}
int main()
{
int t,ca = 1;
cin>>t;
while (t--)
{
int i,j,a,b;
double c;
cin>>n>>m;
memset(head,-1,sizeof(head));
cnt = 0;
for(i = 0; i < m ; i++)
{
scanf("%d %d %lf",&a,&b,&c);
add(Edge(a,b,c));
}
double l =0 , r = 10000009;
for(i = 0; i < 100; i++)
{
double mid = (l+r)/2;
if(ok(mid)) r = mid;
else l =mid;
}
// l = 0.01 * int(l*100);
printf("Case #%d: ",ca++);
if(l > 10000000)
printf("No cycle found.\n");
else
printf("%.2lf\n",l);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: