1423B - Valuable Paper - CodeForces Solution


binary search flows graph matchings graphs *1900

Please click on ads to support us..

C++ Code:

//二分最大边,加上网络流跑二分图匹配
#include<bits/stdc++.h>
using namespace std;
const int N=1e4+10,M=1e5+10,inf=1<<25;
struct Rode
{
    int u,v,t;
}rode[M];
int head[N*3],cent;
int n,m;//点数和边数
int maxflow;
int dep[N*3],ans[N*3],vir[N*3];
struct Node
{
    int ui,vi;
    int ti;
    int next;
}node[M*4];
void add(int u,int v,int t)
{
    node[cent].vi=v;
    node[cent].ti=t;
    node[cent].next=head[u];
    head[u]=cent++;
}
bool bfs()
{//分层
    for(int i=0;i<=2*n+1;i++)
    {
        dep[i]=0x3f3f3f3f;
        ans[i]=0;
        vir[i]=head[i];
    }
    queue<int>q;
    q.push(0);
    dep[0]=0;
    while(!q.empty())
    {
        int now=q.front();
        q.pop();
        ans[now]=0;
        for(int i=head[now];~i;i=node[i].next)
        {
            int to=node[i].vi;
            if(dep[to]>dep[now]+1&&node[i].ti)
            {
                dep[to]=dep[now]+1;
                if(ans[to]==0)
                {
                    ans[to]=1;
                    q.push(to);
                }
            }
        }
    }
    if(dep[2*n+1]!=0x3f3f3f3f) return 1;
    return 0;
}
int dfs(int u,int flow)
{
    int rlow=0;
    if(u==2*n+1)//如果到达了终点
    {
        maxflow+=flow;
        return flow;
    }
    int used=0;
    for(int i=vir[u];~i;i=node[i].next)
    {
        vir[u]=i;
        int d=node[i].vi;
        if(node[i].ti&&dep[d]==dep[u]+1)
        {
            if(rlow=dfs(d,min(flow-used,node[i].ti)))
            {
                used+=rlow;
                node[i].ti-=rlow;
                node[i^1].ti+=rlow;
                if(used==flow) break;
            }
        }
    }
    return used;
}
int Dinic()
{
    while(bfs())
    {
        dfs(0,inf);
    }
    return maxflow;
}
int check(int f)
{
    memset(head,-1,sizeof(head));
    cent=0;
    for(int i=1;i<=n;i++)
    {
        add(0,i,1);//将0作为超级源点,2*n+1作为超级汇点
        add(i,0,0);
        add(n+i,2*n+1,1);
        add(2*n+1,n+i,0);
    }
    for(int i=1;i<=m;i++)
    {
        if(rode[i].t<=f)
        {//如果这条边满足要求
            add(rode[i].u,rode[i].v+n,1);
            add(rode[i].v+n,rode[i].u,0);
        }
    }
    maxflow=0;
    Dinic();
    if(maxflow<n) return 0;
    else return 1; 
}
int main()
{
    scanf("%d%d",&n,&m);
    int minn=inf,maxn=0;
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d%d",&rode[i].u,&rode[i].v,&rode[i].t);
        if(rode[i].t<minn) minn=rode[i].t;
        if(rode[i].t>maxn) maxn=rode[i].t;
    }
    int l=minn,r=maxn;
    int res=-1;
    while(l<=r)
    {
        int mid=(l+r)/2;
        if(check(mid))
        {
            res=mid;
            r=mid-1;
        }
        else l=mid+1;
    }
    printf("%d\n",res);
    //system("pause");
    return 0;
}


Comments

Submit
0 Comments
More Questions

148A - Insomnia cure
1650A - Deletions of Two Adjacent Letters
1512A - Spy Detected
282A - Bit++
69A - Young Physicist
1651A - Playoff
734A - Anton and Danik
1300B - Assigning to Classes
1647A - Madoka and Math Dad
710A - King Moves
1131A - Sea Battle
118A - String Task
236A - Boy or Girl
271A - Beautiful Year
520B - Two Buttons
231A - Team
479C - Exams
1030A - In Search of an Easy Problem
158A - Next Round
71A - Way Too Long Words
160A - Twins
1A - Theatre Square
1614B - Divan and a New Project
791A - Bear and Big Brother
1452A - Robot Program
344A - Magnets
96A - Football
702B - Powers of Two
1036A - Function Height
443A - Anton and Letters