Magical brain-hole solution to a problem --HDU1853 Cyclic Tour

For the maximum matching of bipartite graph with respect to the right and optimal matching of the bipartite graph, B station

CSU ACM-ICPC bigwigs have spoken very clearly.

The needs of the students can go to learn about the B station.

Quietly say, lectures Chen Jiani little sister who was a OIer, who won the silver medal at CTSC2016 (as in HN, did not seem to be able to go into NOI), to explain the way also makes OIers very comfortable.

Title effect: to a directed graph, select any required plurality of rings (repeatable, non-self-ring, may optionally, not connected), such that each point is accessed, and after the minimum sum of edge weights , minimum .

At first glance and bipartite graph it does not matter, but carefully think about, if for each point in the figure split into two (one connection point to this point, a company point out), so as to leave the ring, then the optimal situation there is certainly a point in each ring will be two visits, once all other points, that is not this red fruit on the point of view of the demolition seek the optimal match it?

Suppose we have a FIG: 1 -> 2 -> 3 -> 1, provided split out point i, it is used to connect the point i, is connected to the point i + n (where n = 4 ), then we split a long way point Figure: 5 -> 2,6 -> 3,

7 -> 1, so it is actually a split out of six points in a match exactly.

So, as long as we first determine the point of view of demolition can not form a perfect match, and then rerun it while KM (KM bipartite graph algorithms require the presence of a perfect match, otherwise it will fall into an infinite loop).

Code put it:

#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cstring>
using namespace std;
vector<int>b[101];
int n,m;
struct PE
{
    int s,t,val;
};
E [ 20001 ];
int m [ 101 ] [ 101 ];
bool Function (x, y)
{
    return x.val<y.val;
}
int QWx[101],QWy[101],use[101],pre[101],stx[101];
int visx[101],visy[101],fpre[101];
bool Hungary(int x)
{
    int temp=0;
    visx[x]=1;
    for(int i=0;i<b[x].size();i++)
    {
        int to=b[x][i];
        if(visy[to])
        {
            continue;
        }
        temp=QWx[x]+QWy[to]-mp[x][to];
        if(temp==0)
        {
            visy[to]=1;
            if(pre[to]==0||Hungary(pre[to]))
            {
                fpre[x]=to;
                pre[to]=x;
                return 1;
            }
        }
        presence 
            STX [TO] = min (STX [TO], temp);
    }
    return 0;
}
bool PDHungary(int x)
{
    for(int i=0;i<b[x].size();i++)
    {
        int to=b[x][i];
        if(visy[to])
            continue;
        visy[to]=1;
        if(pre[to]==0||PDHungary(pre[to]))
        {
            pre[to]=x;
            return 1;
        }
    }
    return 0;
}
void KM()
{
    for(int i=1;i<=n;i++)
    {
        memset(stx,0x3f3f3f3f,sizeof(stx));
        
        while(1)
        {
            memset(visx,0,sizeof(visx));
            memset(visy,0,sizeof(visy));
            if(Hungary(i))
            {
                break;
            }
            else
            {
                int date=0x3f3f3f3f;
                for(int j=1;j<=n;j++)
                {
                    if(!visy[j])
                    date=min(date,stx[j]);
                }
                for(int j=1;j<=n;j++)
                {
                    if(visx[j])
                        QWx[j]-=date;
                    if(visy[j])
                        QWy[j]+=date;
                    else
                        stx[j]-=date;
                }
            }
        }
    }
}
int Linyin ()
{
    //scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        b[i].clear();
    }
    int years = 0 ;
    memset(mp,-0x3f3f3f3f,sizeof(mp));
    memset(QWx,-0x3f3f3f3f,sizeof(QWx));
    memset(QWy,0,sizeof(QWy));
    memset(pre,0,sizeof(pre));
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d%d",&E[i].s,&E[i].t,&E[i].val);
        E[i].val*=-1;
    }
    sort(E+1,E+1+m,Function);
    mp [E [ 1 ] .s] [E [ 1 ] .t] = E [ 1 ] .val;
    b[E[1].s].push_back(E[1].t);
    for(int i=2;i<=m;i++)
    {
        if(E[i].s==E[i-1].s&&E[i].t==E[i-1].t&&E[i].val==E[i-1].val)
            continue;
        mp [E [i] .s] [E [i] .t] = max (mp [E [i] .s] [E [i] .t], E [i] .val);
        b[E[i].s].push_back(E[i].t);
        QWx[E[i].s]=max(QWx[E[i].s],E[i].val);    
    }
    for(int i=1;i<=n;i++)
    {
        memset(visy,0,sizeof(visy));
        if(!PDHungary(i))
        {
            printf("%d\n",-1);
            return 0;
        }
    }
    memset(pre,0,sizeof(pre));
    memset(visy,0,sizeof(visy));
    KM();
    for(int i=1;i<=n;i++)
    {
        years + = mp [pre [i]] [i];
    } 
    years * = - 1 ;
    printf("%d\n",ans);
    return 0;
}
int main ()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        LINYIN ();
    }
    return 0;
}

 

End Sahua!

 

Guess you like

Origin www.cnblogs.com/XLINYIN/p/11784852.html