zoj 3792 Romantic Value

题目链接

求最小割的值, 以及割边最少的情况的边数。

先求一遍最小割, 然后把所有割边的权值变为1, 其他边变成inf, 在求一遍最小割, 此时求出的就是最少边数。

Inf打成inf  WA了好几发............

 #include<bits/stdc++.h>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, a, n) for(int i = a; i<n; i++)
#define ull unsigned long long
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = 1e4;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
const int maxn = 2e5+;
int num, q[maxn*], head[maxn*], dis[maxn*], s, t;
struct node
{
int to, nextt, c;
node(){}
node(int to, int nextt, int c):to(to), nextt(nextt), c(c){}
}e[maxn*];
int bfs() {
mem(dis);
int st = , ed = ;
dis[s] = ;
q[ed++] = s;
while(st<ed) {
int u = q[st++];
for(int i = head[u]; ~i; i = e[i].nextt) {
int v = e[i].to;
if(!dis[v]&&e[i].c) {
dis[v] = dis[u]+;
if(v == t)
return ;
q[ed++] = v;
}
}
}
return ;
}
int dfs(int u, int limit) {
int cost = ;
if(u == t)
return limit;
for(int i = head[u]; ~i; i = e[i].nextt) {
int v = e[i].to;
if(e[i].c&&dis[v] == dis[u]+) {
int tmp = dfs(v, min(e[i].c, limit-cost));
if(tmp>) {
e[i].c -= tmp;
e[i^].c += tmp;
cost += tmp;
if(cost == limit)
break;
} else {
dis[v] = -;
}
}
}
return cost;
}
int dinic() {
int ans = ;
while(bfs()) {
ans += dfs(s, inf);
}
return ans;
}
void add(int u, int v, int c) {
e[num] = node(v, head[u], c); head[u] = num++;
e[num] = node(u, head[v], c); head[v] = num++;
}
void init() {
mem1(head);
num = ;
}
int main()
{
int T, m, n, x, y, q, p, z;
cin>>T;
while(T--) {
init();
int sum = ;
scanf("%d%d%d%d", &n, &m, &p, &q);
s = p, t = q;
while(m--) {
scanf("%d%d%d", &x, &y, &z);
add(x, y, z);
sum += z;
}
int ans = dinic();
if(ans == ) {
cout<<"Inf"<<endl;
continue;
}
sum -= ans;
for(int i = ; i<num; i+=) {
if(e[i].c == ) {
e[i].c = ;
e[i^].c = inf;
} else if(e[i^].c==) {
e[i].c = inf;
e[i^].c = ;
} else {
e[i].c = e[i^].c = inf;
}
}
ans = dinic();
printf("%.2f\n", 1.0*sum/ans);
}
}
上一篇:Android View 的事件分发原理解析


下一篇:在AndroidStudio中使用单元测试