Cyclic Tour (hdu 1853 二分圖最小權問題),cyclichdu

來源:互聯網
上載者:User

Cyclic Tour (hdu 1853 二分圖最小權問題),cyclichdu
Cyclic TourTime Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others)
Total Submission(s): 1883    Accepted Submission(s): 941


Problem DescriptionThere 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?
 
InputThere 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).
 
OutputOutput 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-1Hint In the first sample, there are two cycles, (1->2->3->1) and (6->5->4->6) whose length is 20 + 22 = 42.  
 
AuthorRoBa@TJU 
SourceHDU 2007 Programming Contest - Final 
Recommendlcy   |   We have carefully selected several similar problems for you:  3395 3315 1565 2448 3376 
題意:給出n個城市和m條路,有向圖,現在Tom想遊遍這n個城市,滿足路線是若干個迴路,要求每個城市要在一個迴路中,每條邊有一定的旅遊花費,問Tom遊遍所有城市的最小旅遊花費。思路:二分圖最小權問題,把邊權取反,用KM演算法。代碼:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <string>#include <map>#include <stack>#include <vector>#include <set>#include <queue>#pragma comment (linker,"/STACK:102400000,102400000")#define maxn 1005#define MAXN 2005#define mod 1000000009#define INF 0x3f3f3f3f#define pi acos(-1.0)#define eps 1e-6#define lson rt<<1,l,mid#define rson rt<<1|1,mid+1,r#define FRE(i,a,b)  for(i = a; i <= b; i++)#define FREE(i,a,b) for(i = a; i >= b; i--)#define FRL(i,a,b)  for(i = a; i < b; i++)#define FRLL(i,a,b) for(i = a; i > b; i--)#define mem(t, v)   memset ((t) , v, sizeof(t))#define sf(n)       scanf("%d", &n)#define sff(a,b)    scanf("%d %d", &a, &b)#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)#define pf          printf#define DBG         pf("Hi\n")typedef long long ll;using namespace std;/*KM演算法 O(nx*nx*ny)求最大權匹配(首選)若求最小權匹配,可將權值取相反數,結果取相反數點的標號從0開始*/const int N=110;int nx,ny;      //兩邊的點數int g[N][N];    //二分圖描述,g賦初值為-INFint linker[N],lx[N],ly[N];  //y 中各點匹配狀態,x,y中的點的標號int slack[N];bool visx[N],visy[N];bool flag;bool DFS(int x){    visx[x]=true;    for (int y=0;y<ny;y++)    {        if (visy[y]) continue;        int tmp=lx[x]+ly[y]-g[x][y];        if (tmp==0)        {            visy[y]=true;            if (linker[y]==-1||DFS(linker[y]))            {                linker[y]=x;                return true;            }        }        else if (slack[y]>tmp)            slack[y]=tmp;    }    return false;}int KM(){    flag=true;    memset(linker,-1,sizeof(linker));    memset(ly,0,sizeof(ly));    for (int i=0;i<nx;i++) //賦初值,lx置為最大值    {        lx[i]=-INF;        for (int j=0;j<ny;j++)        {            if (g[i][j]>lx[i])                lx[i]=g[i][j];        }    }    for (int x=0;x<nx;x++)    {        for (int i=0;i<ny;i++)            slack[i]=INF;        while (true)        {            memset(visx,false,sizeof(visx));            memset(visy,false,sizeof(visy));            if (DFS(x)) break;            int d=INF;            for (int i=0;i<ny;i++)                if (!visy[i]&&d>slack[i])                    d=slack[i];            for (int i=0;i<nx;i++)                if (visx[i])                    lx[i]-=d;            for (int i=0;i<ny;i++)            {                if (visy[i])                    ly[i]+=d;                else                    slack[i]-=d;            }        }    }    int res=0;    for (int i=0;i<ny;i++)    {        if (linker[i]==-1||g[linker[i]][i]<=-INF) //有的點不能匹配的話return-1        {            flag=false;            break;        }        res+=g[linker[i]][i];    }    return res;}int n,m;int main(){#ifndef ONLINE_JUDGE    freopen("C:/Users/asus1/Desktop/IN.txt","r",stdin);#endif    int i,j;    while (~sff(n,m))    {        for (i=0;i<=n;i++)            for (j=0;j<=n;j++)                g[i][j]=-INF;        nx=ny=n;        int u,v,w;        for (i=0;i<m;i++)        {            sfff(u,v,w);            u--;            v--;            if (-w>g[u][v])                g[u][v]=-w;        }        int ans=KM();        ans=-ans;        if (!flag)            pf("-1\n");        else            pf("%d\n",ans);    }    return 0;}


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.