题目:
http://acm.hdu.edu.cn/showproblem.php?pid=3987
题意:
给一个网络,边有有向边和无向边两种,挑选一些边破坏,使得从0无法到达n-1,每条边都有一个破环的成本,求在成本最低的情况下需要破坏的最少边数
思路:
明显最小成本就是最小割,求边数的话,扩大边权然后取余即可
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1000 + 10;
const ll INF = 0x3f3f3f3f3f3f3f3f;
struct edge
{
int to, next;
ll cap;
}g[N*N*2];
int level[N], cur[N], pre[N], gap[N];
int cnt, head[N];
int nv;
void init()
{
cnt = 0;
memset(head, -1, sizeof head);
}
void add_edge(int v, int u, ll cap)
{
g[cnt].to = u, g[cnt].cap = cap, g[cnt].next = head[v], head[v] = cnt++;
g[cnt].to = v, g[cnt].cap = 0, g[cnt].next = head[u], head[u] = cnt++;
}
ll sap(int s, int t)
{
memset(level, 0, sizeof level);
memset(gap, 0, sizeof gap);
memcpy(cur, head, sizeof head);
gap[0] = nv;
int v = pre[s] = s;
ll flow = 0, aug = INF;
while(level[s] < nv)
{
bool flag = false;
for(int &i = cur[v]; i != -1; i = g[i].next)
{
int u = g[i].to;
if(g[i].cap > 0 && level[v] == level[u] + 1)
{
flag = true;
pre[u] = v;
v = u;
aug = min(aug, g[i].cap);
if(v == t)
{
flow += aug;
while(v != s)
{
v = pre[v];
g[cur[v]].cap -= aug;
g[cur[v]^1].cap += aug;
}
aug = INF;
}
break;
}
}
if(flag) continue;
int minlevel = nv;
for(int i = head[v]; i != -1; i = g[i].next)
{
int u = g[i].to;
if(g[i].cap > 0 && level[u] < minlevel)
minlevel = level[u], cur[v] = i;
}
if(--gap[level[v]] == 0) break;
level[v] = minlevel + 1;
gap[level[v]]++;
v = pre[v];
}
return flow;
}
int main()
{
int t, n, m, cas = 0;
scanf("%d", &t);
while(t--)
{
init();
scanf("%d%d", &n, &m);
int a, b, c, d;
for(int i = 1; i <= m; i++)
{
scanf("%d%d%d%d", &a, &b, &c, &d);
a++, b++;
add_edge(a, b, 1LL*c*(m+1) + 1);
if(d) add_edge(b, a, 1LL*c*(m+1) + 1);//边权扩大(m+1)倍再+1
}
int ss = 1, tt = n;
nv = n;
printf("Case %d: %lld\n", ++cas, sap(ss, tt) % (m+1));
}
return 0;
}