Tour HDU

来源:互联网 发布:西瓜影音播放网络文件 编辑:程序博客网 时间:2024/05/18 01:25

In the kingdom of Henryy, there are N (2 <= N <= 200) cities, with M (M <= 30000) one-way roads connecting them. You are lucky enough to have a chance to have a tour in the kingdom. The route should be designed as: The route should contain one or more loops. (A loop is a route like: A->B->……->P->A.)
Every city should be just in one route.
A loop should have at least two cities. In one route, each city should be visited just once. (The only exception is that the first and the last city should be the same and this city is visited twice.)
The total distance the N roads you have chosen should be minimized.
Input
An integer T in the first line indicates the number of the test cases.
In each test case, the first line contains two integers N and M, indicating the number of the cities and the one-way roads. Then M lines followed, each line has three integers U, V and W (0 < W <= 10000), indicating that there is a road from U to V, with the distance of W.
It is guaranteed that at least one valid arrangement of the tour is existed.
A blank line is followed after each test case.
Output
For each test case, output a line with exactly one integer, which is the minimum total distance.
Sample Input
1
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
Sample Output
42
这题就是说,每个点都必须属于某个圈,实际上这题是用二分匹配的的思想,而实现则是用最小花费最大流,首先把每个点给拆了两个,然后来建图,因为保证至少有圈的,所以每个点一定都有匹配边,这个思想可参考hihoCoder的网络流部分,讲的很好,建完图跑个最小花费最大流就好,这题无限超时,看网上说是要去重边,我去~~~~

#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<queue>using namespace std;const int maxn=405;//点的最大上线const int maxx=61005;//边的最大上线const int INF=1<<30;int to[maxx],nex[maxx],cap[maxx],flow[maxx],cost[maxx];int head[maxn];int pre[maxn],dis[maxn];int eid;void init()//每个样例都需要使用一次{    eid=0;    memset(head,-1,sizeof(head));}void addEdge(int u,int v,int ca,int co)//建边{    int i;    for(i=head[u];~i;i=nex[i])        if(to[i]==v)        break;    if(~i)    {        if(cost[i]>co)            cost[i]=co,cost[i^1]=-co;        return;    }    to[eid]=v;    cap[eid]=ca;    cost[eid]=co;    flow[eid]=0;    nex[eid]=head[u];    head[u]=eid++;    to[eid]=u;    cap[eid]=0;    cost[eid]=-co;    flow[eid]=0;    nex[eid]=head[v];    head[v]=eid++;}bool inQ[maxn];bool spfa(int s,int e,int n)//最短路算法代替广搜{    queue<int> que;    memset(inQ,false,sizeof(inQ));    for(int i=0;i<=n;i++)        dis[i]=INF;    dis[s]=0,inQ[s]=true;    pre[s]=-1;    que.push(s);    while(!que.empty())    {        int u=que.front();        que.pop();        inQ[u]=false;        for(int i=head[u];~i;i=nex[i])        {            int v=to[i];            if(cap[i]-flow[i]>0&&dis[v]>dis[u]+cost[i])            {                dis[v]=dis[u]+cost[i];                pre[v]=i;//这是为了修改边的容量                if(!inQ[v])                    que.push(v),inQ[v]=true;            }        }    }    return dis[e]!=INF;}int minCostMaxFlow(int s,int e,int &minCost,int n)//建完边使用{    int ans=0;    while(spfa(s,e,n))    {        ans+=1;        for(int i=pre[e];~i;i=pre[to[i^1]])        {            flow[i]+=1;flow[i^1]-=1;            minCost+=cost[i];        }    }    return ans;}int main(){    int t;    scanf("%d",&t);    int n,m;    int x,y,w;    while(t--)    {        scanf("%d%d",&n,&m);        init();        while(m--)        {            scanf("%d%d%d",&x,&y,&w);            addEdge(x,n+y,1,w);        }        for(int i=1;i<=n;i++)            addEdge(0,i,1,0),addEdge(n+i,n+n+1,1,0);        int minCost=0;        minCostMaxFlow(0,n+n+1,minCost,n+n+2);        printf("%d\n",minCost);    }    return 0;}