【刷题】HDU 1853 Cyclic Tour

Problem Description

There are N cities in our country, and M one-way roads connecting them. Now Little Tom wants to make several cyclic tours, which satisfy that, each cycle contain at least two cities, and each city belongs to one cycle exactly. Tom wants the total length of all the tours minimum, but he is too lazy to calculate. Can you help him?

Input

There are several test cases in the input. You should process to the end of file (EOF).
The first line of each test case contains two integers N (N ≤ 100) and M, indicating the number of cities and the number of roads. The M lines followed, each of them contains three numbers A, B, and C, indicating that there is a road from city A to city B, whose length is C. (1 ≤ A,B ≤ N, A ≠ B, 1 ≤ C ≤ 1000).

Output

Output one number for each test case, indicating the minimum length of all the tours. If there are no such tours, output -1.

Sample Input

6 9
1 2 5
2 3 5
3 1 10
3 4 12
4 1 8
4 6 11
5 4 7
5 6 9
6 5 4
6 5
1 2 1
2 3 1
3 4 1
4 5 1
5 6 1

Sample Output

42
-1

Hint

In the first sample, there are two cycles, (1->2->3->1) and (6->5->4->6) whose length is 20 + 22 = 42.

Description(CHN)

给你一个 \(N\) 个点 \(M\) 条边的带权有向图,现在要你求这样一个值:

该有向图中的所有顶点正好被1个或多个不相交的有向环覆盖.

这个值就是 所有这些有向环的权值和. 要求该值越小越好.

Solution

有向图环覆盖,变成二分图匹配,经典套路
然后要求权最小,就是二分图最大匹配,跑遍费用流就好了

#include<bits/stdc++.h>
#define ui unsigned int
#define ll long long
#define db double
#define ld long double
#define ull unsigned long long
const int MAXN=100+10,MAXM=MAXN*MAXN,inf=0x3f3f3f3f;
int n,m,e,beg[MAXN<<1],s,t,level[MAXN<<1],nex[MAXM<<1],to[MAXM<<1],cap[MAXM<<1],was[MAXM<<1],p[MAXN<<1],cur[MAXN<<1],vis[MAXN<<1],clk,answas;
std::queue<int> q;
template<typename T> inline void read(T &x)
{
    T data=0,w=1;
    char ch=0;
    while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
    if(ch=='-')w=-1,ch=getchar();
    while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();
    x=data*w;
}
template<typename T> inline void write(T x,char ch='\0')
{
    if(x<0)putchar('-'),x=-x;
    if(x>9)write(x/10);
    putchar(x%10+'0');
    if(ch!='\0')putchar(ch);
}
template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);}
template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);}
template<typename T> inline T min(T x,T y){return x<y?x:y;}
template<typename T> inline T max(T x,T y){return x>y?x:y;}
inline void insert(int x,int y,int z,int k)
{
    to[++e]=y;
    nex[e]=beg[x];
    beg[x]=e;
    cap[e]=z;
    was[e]=k;
    to[++e]=x;
    nex[e]=beg[y];
    beg[y]=e;
    cap[e]=0;
    was[e]=-k;
}
inline bool bfs()
{
    for(register int i=1;i<=t;++i)level[i]=inf;
    level[s]=0;
    p[s]=1;
    q.push(s);
    while(!q.empty())
    {
        int x=q.front();
        q.pop();
        p[x]=0;
        for(register int i=beg[x];i;i=nex[i])
            if(cap[i]&&level[to[i]]>level[x]+was[i])
            {
                level[to[i]]=level[x]+was[i];
                if(!p[to[i]])p[to[i]]=1,q.push(to[i]);
            }
    }
    return level[t]!=inf;
}
inline int dfs(int x,int maxflow)
{
    if(x==t||!maxflow)return maxflow;
    int res=0;
    vis[x]=clk;
    for(register int &i=cur[x];i;i=nex[i])
        if((vis[x]^vis[to[i]])&&cap[i]&&level[to[i]]==level[x]+was[i])
        {
            int f=dfs(to[i],min(maxflow,cap[i]));
            res+=f;
            cap[i]-=f;
            cap[i^1]+=f;
            answas+=was[i]*f;
            maxflow-=f;
            if(!maxflow)break;
        }
    vis[x]=0;
    return res;
}
inline int MCMF()
{
    int res=0;
    while(bfs())clk++,memcpy(cur,beg,sizeof(cur)),res+=dfs(s,inf);
    return res;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        e=1;memset(beg,0,sizeof(beg));answas=0;
        for(register int i=1;i<=m;++i)
        {
            int u,v,k;read(u);read(v);read(k);
            insert(v,u+n,1,k);
        }
        s=n+n+1,t=s+1;
        for(register int i=1;i<=n;++i)insert(s,i,1,0),insert(i+n,t,1,0);
        if(MCMF()!=n)puts("-1");
        else write(answas,'\n');
    }
    return 0;
}

转载于:https://www.cnblogs.com/hongyj/p/9418066.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值