HDU1853 Cyclic Tour

来源:互联网 发布:unity3d数学基础 编辑:程序博客网 时间:2024/05/16 18:57

Cyclic Tour

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


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 91 2 52 3 53 1 103 4 124 1 84 6 115 4 75 6 96 5 46 51 2 12 3 13 4 14 5 15 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
RoBa@TJU
 

Source
HDU 2007 Programming Contest - Final
 

Recommend
lcy

——————————————————————————————

题目的意思是是给出一张有向图,要选择几条边使得每个点都落在一个环上,使得所选的边和最小

思路:每个点落在环上,所以每个点的入度出度均为1,这正好符合二分图性质,建立二分图,求最大权匹配,题目要求最小,权值取负数即可

#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <cmath>#include <algorithm>#include <queue>#include <vector>#include <set>#include <stack>#include <map>#include <climits>using namespace std;#define LL long longconst int MAXN = 505;const int INF = 0x3f3f3f3f;int g[MAXN][MAXN];int lx[MAXN],ly[MAXN]; //顶标int linky[MAXN];int visx[MAXN],visy[MAXN];int slack[MAXN];int nx,ny;bool find(int x){    visx[x] = true;    for(int y = 0; y < ny; y++)    {        if(visy[y])            continue;        int t = lx[x] + ly[y] - g[x][y];        if(t==0)        {            visy[y] = true;            if(linky[y]==-1 || find(linky[y]))            {                linky[y] = x;                return true;        //找到增广轨            }        }        else if(slack[y] > t)            slack[y] = t;    }    return false;                   //没有找到增广轨(说明顶点x没有对应的匹配,与完备匹配(相等子图的完备匹配)不符)}int KM()                //返回最优匹配的值{    int i,j;    memset(linky,-1,sizeof(linky));    memset(ly,0,sizeof(ly));    for(i = 0; i < nx; i++)        for(j = 0,lx[i] = -INF; j < ny; j++)            lx[i] = max(lx[i],g[i][j]);    for(int x = 0; x < nx; x++)    {        for(i = 0; i < ny; i++)            slack[i] = INF;        while(true)        {            memset(visx,0,sizeof(visx));            memset(visy,0,sizeof(visy));            if(find(x))                     //找到增广轨,退出                break;            int d = INF;            for(i = 0; i < ny; i++)          //没找到,对l做调整(这会增加相等子图的边),重新找            {                if(!visy[i] && d > slack[i])                    d = slack[i];            }            for(i = 0; i < nx; i++)            {                if(visx[i])                    lx[i] -= d;            }            for(i = 0; i < ny; i++)            {                if(visy[i])                    ly[i] += d;                else                    slack[i] -= d;            }        }    }    int result = 0;    int cnt=0;    for(i = 0; i < ny; i++)        if(linky[i]>-1)        {             result += g[linky[i]][i];             if(g[linky[i]][i]!=-1044266559)             cnt++;        }           if(cnt<nx)            result=1;    return -result;}int main(){    int n,m,u,v,c,T;    while(~scanf("%d%d",&n,&m))    {        nx=ny=n;        memset(g,-INF,sizeof g);        for(int i=0; i<m; i++)        {            scanf("%d%d%d",&u,&v,&c);            u--,v--;            g[u][v]=max(g[u][v],-c);        }        printf("%d\n",KM());    }    return 0;}


原创粉丝点击