1. 程式人生 > >hdu 1853Cyclic Tour網路流 有向環覆蓋

hdu 1853Cyclic Tour網路流 有向環覆蓋

Cyclic Tour

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others)
Total Submission(s): 3235    Accepted Submission(s): 1689


 

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.

 

 

Author

[email protected]

 

 

Source

HDU 2007 Programming Contest - Final

 

 

Recommend

lcy

 

給你一個N個點M條邊的帶權有向圖,現在要你求這樣一個值:

該有向圖中的所有頂點正好被1個或多個不相交的有向環覆蓋(每個節點只能被一個有向環包含).

這個值就是 所有這些有向環的權值和. 要求該值越小越好.

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>

#include<vector>
#include<queue>
#define inf 1e9
#define maxn 205
using namespace std;
struct edge
{
    int from,to,cap,flow,cost;
    edge()
    {

    }
    edge(int f,int t,int ca,int fl,int co):from(f),to(t),cap(ca),flow(fl),cost(co){}
};
struct mcnf
{
    int n,m,s,t;
    vector<edge>edges;
    vector<int>g[maxn];
    bool inq[maxn];
    int d[maxn];
    int p[maxn];
    int a[maxn];
    void init(int n,int s,int t)
    {
        this->n=n;
        this->s=s;
        this->t=t;
        edges.clear();
        for(int i=0;i<n;i++)
        g[i].clear();
    }
    void addedge(int from,int to,int cap,int cost)
    {
        edges.push_back(edge(from,to,cap,0,cost));
        edges.push_back(edge(to,from,0,0,-cost));
        m=edges.size();
        g[from].push_back(m-2);
        g[to].push_back(m-1);
    }
    bool bellford(int &flow,int &cost)
    {for(int i=0;i<n;i++)
    d[i]=inf;
    memset(inq,0,sizeof(inq));
    d[s]=0;
    a[s]=inf;
    inq[s]=true;
    p[s]=0;
    queue<int>q;
    q.push(s);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        inq[u]=false;
        for(int i=0;i<g[u].size();i++)
        {
            edge&e=edges[g[u][i]];
            if(e.cap>e.flow&&d[e.to]>d[u]+e.cost)
            {
                d[e.to]=d[u]+e.cost;
                p[e.to]=g[u][i];
                a[e.to]=min(a[u],e.cap-e.flow);
                if(!inq[e.to])
                {
                    q.push(e.to);
                    inq[e.to]=true;
                }
            }
        }

    }
    if(d[t]==inf)
    return false;
    flow+=a[t];
    cost+=a[t]*d[t];
    int u=t;
    while(u!=s)
    {
        edges[p[u]].flow+=a[t];
        edges[p[u]^1].flow-=a[t];
        u=edges[p[u]].from;

    }
    return true;

    }
    int mincost(int num)
    {
        int flow=0,cost=0;
        while(bellford(flow,cost));
        return flow==num?cost:-1;


    }
}MM;
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        int src=0,dst=2*n+1;
        MM.init(2*n+2,src,dst);
        for(int i=1;i<=n;i++)
          {MM.addedge(src,i,1,0);
          MM.addedge(i+n,dst,1,0);

    }
    while(m--)
    {
        int u,v,w;
        scanf("%d%d%d",&u,&v,&w);
        MM.addedge(u,v+n,1,w);

    }
    printf("%d\n",MM.mincost(n));
    }
    return 0;

}