UVA - 11090 - Going in Cycle!!(二分+差分约束系统)

时间:2023-03-10 00:33:36
UVA - 11090 - Going in Cycle!!(二分+差分约束系统)

Problem  UVA - 11090 - Going in Cycle!!

Time Limit: 3000 mSec

UVA - 11090 - Going in Cycle!!(二分+差分约束系统)Problem 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.

UVA - 11090 - Going in Cycle!!(二分+差分约束系统)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

UVA - 11090 - Going in Cycle!!(二分+差分约束系统)Sample Input

2 2 1 1 2 1 2 2 1 2 2 2 1 3

UVA - 11090 - Going in Cycle!!(二分+差分约束系统)Sample Output

Case #1: No cycle found.

Case #2: 2.50

题解:差分约束系统板子题,配上二分很容易解决,这里的spfa和一般的spfa稍有区别,原因我在UVA 11478的博客里解释过了,不再赘述,这种写法会比分别以每个点为源点跑高效不少,目前这份代码耗时50ms,但是分别以每个点为源点跑了200ms。

 #include <bits/stdc++.h>

 using namespace std;

 #define REP(i, n) for (int i = 1; i <= (n); i++)
#define sqr(x) ((x) * (x)) const int maxn = + ;
const int maxm = + ;
const int maxs = + ; typedef long long LL;
typedef pair<int, int> pii;
typedef pair<double, double> pdd; const LL unit = 1LL;
const int INF = 0x3f3f3f3f;
const LL mod = ;
const double eps = 1e-;
const double inf = 1e15;
const double pi = acos(-1.0); struct Edge
{
int to, next;
double w;
} edge[maxm << ]; int n, m;
int tot, head[maxn]; void init()
{
tot = ;
memset(head, -, sizeof(head));
} void AddEdge(int u, int v, double w)
{
edge[tot].to = v;
edge[tot].next = head[u];
edge[tot].w = w;
head[u] = tot++;
} double dist[maxn];
bool vis[maxn];
int cnt[maxn]; bool spfa()
{
queue<int> que;
for (int i = ; i < n; i++)
{
dist[i] = ;
cnt[i] = ;
vis[i] = true;
que.push(i);
} while (!que.empty())
{
int x = que.front();
que.pop();
vis[x] = false;
for (int i = head[x]; i != -; i = edge[i].next)
{
int v = edge[i].to;
if (dist[v] > dist[x] + edge[i].w)
{
dist[v] = dist[x] + edge[i].w;
if (!vis[v])
{
que.push(v);
vis[v] = true;
if (++cnt[v] > n)
{
return true;
}
}
}
}
}
return false;
} bool Judge(double x)
{
for (int i = ; i < tot; i++)
{
edge[i].w -= x;
}
bool ok = true;
if(spfa())
ok = false;
for (int i = ; i < tot; i++)
{
edge[i].w += x;
}
return ok;
} int iCase; int main()
{
ios::sync_with_stdio(false);
cin.tie();
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int T;
cin >> T;
while (T--)
{
cin >> n >> m;
init();
int u, v;
double w;
double lim = -inf;
for (int i = ; i < m; i++)
{
cin >> u >> v >> w;
u--, v--;
AddEdge(u, v, w);
lim = max(lim, w);
}
cout << "Case #" << ++iCase << ": ";
if (Judge(lim + ))
{
cout << "No cycle found." << endl;
}
else
{
double le = , ri = lim;
while (ri - le > 1e-)
{
double mid = (le + ri) / ;
if (Judge(mid))
{
le = mid;
}
else
{
ri = mid;
}
}
cout << fixed << setprecision() << le << endl;
}
}
return ;
}