【 UVALive - 2197】Paint the Roads(上下界费用流)

时间:2022-03-19 19:22:32

Description

In a country there are n cities connected by m one way roads. You can paint any of these roads. To paint a road it costs d unit of money where d is the length of that road. Your task is to paint some of the roads so that the painted roads can be partitioned into some disjoint cycles such that every vertex appears in exactly k of these disjoint cycles. But you have to minimize the costs of painting these roads.

Input

First line of the input contains T the number of test case. Then following lines contains T Test cases. Each case starts with a line containing 3 integers n (1 ≤ n ≤ 40), m (1 ≤ m ≤ 2000) and k (1 ≤ k and 1 ≤ k ∗ n ≤ 100). Next m lines contain description of m roads. Each line contains three integers f, t (0 ≤ f, t < n and f ̸= t) and d (0 ≤ d < 100). That means there is a road of d length from city f to city t. You can assume that there will be at most one road in one direction between two cities.

Output 

For each test case output contains 1 integer denoting the minimum unit of money needed to paint roads. In the case it is impossible to paint the roads maintaining the constraints output ‘-1’.

Sample Input
4
4 8 1
0 1 1
1 0 2
2 3 1
3 2 2
0 2 5
2 0 6
1 3 5
3 1 6
4 8 1
0 1 1
1 0 10
2 3 10
3 2 1
0 2 10
2 0 1
1 3 1
3 1 10
4 8 2
0 1 1
1 0 2
2 3 1
3 2 2
0 2 5
2 0 6
1 3 5
3 1 6
3 4 1
0 1 5
1 0 6
0 2 7
2 0 8

Sample Output
6
4
28
-1

【题意】

有n个点,m条边的有向图,选一些边使得这些边组成若干无公共边的回路(回路不经过重复的点),使得每个点恰好在k个回路上,求满足条件的最小边权和。

【分析】

 普通的就拆边,加费用跑费用流判满流。对于新加的约束——每个点必须经过k遍,就把一个点拆成两个点,建上下界流量都为k,一个点只连入边,一个点只连出边即可。

代码如下:

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define Maxn 110
#define Maxm 110000
#define INF 0xfffffff struct node
{
int x,y,f,c,o,next;
}t[Maxm];int len; int st,ed,sum;
int dis[Maxn],pre[Maxn],flow[Maxn],first[Maxn];
bool inq[Maxn]; int mymin(int x,int y) {return x<y?x:y;} void ins(int x,int y,int f,int c)
{
if(f==) return ;
if(x==st) sum+=f;
t[++len].x=x;t[len].y=y;t[len].f=f;t[len].c=c;
t[len].next=first[x];first[x]=len;t[len].o=len+;
t[++len].x=y;t[len].y=x;t[len].f=;t[len].c=-c;
t[len].next=first[y];first[y]=len;t[len].o=len-;
} void make_edge(int x,int y,int k1,int k2,int c)
{
ins(st,y,k2,c);
ins(x,ed,k2,);
ins(y,x,k2-k1,-c);
} queue<int > q;
bool bfs()
{
while(!q.empty()) q.pop();
memset(pre,-,sizeof(pre));
memset(inq,,sizeof(inq));
memset(dis,,sizeof(dis));
pre[st]=;flow[st]=INF;inq[st]=;
dis[st]=;q.push(st);
while(!q.empty())
{
int x=q.front(),y,i;
for(i=first[x];i;i=t[i].next) if(t[i].f>)
{
y=t[i].y;
if(dis[y]>dis[x]+t[i].c)
{
pre[y]=i;
dis[y]=dis[x]+t[i].c;
flow[y]=mymin(t[i].f,flow[x]);
if(!inq[y]) {q.push(y);inq[y]=;}
}
}
q.pop();inq[x]=;
}
if(pre[ed]==-) return ;
return flow[ed];
} void max_flow()
{
int ans=,a,h=;
while(a=bfs())
{
int now=ed;ans+=a*dis[ed];
while(now!=st)
{
t[pre[now]].f-=a;
t[t[pre[now]].o].f+=a;
now=t[pre[now]].x;
}
h+=a;
}
if(sum!=h) printf("-1\n");
else printf("%d\n",ans);
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,m,k;
scanf("%d%d%d",&n,&m,&k);
memset(first,,sizeof(first));
len=;sum=;
st=*n+;ed=st+;
for(int i=;i<=m;i++)
{
int x,y,c;
scanf("%d%d%d",&x,&y,&c);
x++;y++;
make_edge(x+n,y,,,c);
}
for(int i=;i<=n;i++) make_edge(i,i+n,k,k,);
max_flow();
}
return ;
}

[LA2197]

2016-06-10 14:59:16