zoukankan      html  css  js  c++  java
  • nyoj 310二分+dinic

    #include<stdio.h>
    #include<queue>
    #include<string.h>
    using namespace std;
    #define inf 0x7fffffff
    #define N 300
    int p,t,n;
    int Min(int a,int b) {
    return a>b?b:a;
    }
    struct node {
       int u,v,w,f;
    }mp[40001];
    int flow[N][N],d[N];
    int bfs() {
     int cur,i;
     queue<int>q;
     memset(d,0,sizeof(d));
     d[1]=1;
       q.push(1);
        while(!q.empty()) {
            cur=q.front();
            q.pop();
            for(i=1;i<=n;i++)
         if(flow[cur][i]&&!d[i]) {
              d[i]=d[cur]+1;
              if(i==n)
                return 1;
               q.push(i);
         }
        }
        return 0;
    }
    int dfs(int u,int limit) {
        if(u==n)
            return limit;
      int cost=0,i,k;
     for(i=1;i<=n;i++)
        if(d[i]==d[u]+1&&flow[u][i]) {
            k=dfs(i,Min(limit-cost,flow[u][i]));
            if(k>0) {
                flow[u][i]-=k;
                flow[i][u]+=k;
                cost+=k;
                if(cost==limit)
                    break;
            }
            else
                d[i]=-1;
        }
        return cost;
    }
    int dinic (int mid) {
       int ans=0,i;
        memset(flow,0,sizeof(flow));
      for(i=1;i<=p;i++)
        if(mp[i].w<=mid) {
       flow[mp[i].u][mp[i].v]++;
       flow[mp[i].v][mp[i].u]++;
        }
       while(bfs())
        ans+=dfs(1,inf);
         if(ans>=t)
            return 1;
         return 0;
    }
    int main() {
          int i,min,max,mid;
          while(scanf("%d%d%d",&n,&p,&t)!=EOF) {
                min=inf;
          max=-1;
             for(i=1;i<=p;i++) {
                scanf("%d%d%d",&mp[i].u,&mp[i].v,&mp[i].w);
                if(mp[i].w>max)
                    max=mp[i].w;
                if(mp[i].w<min)
                    min=mp[i].w;
             }
              while(min<=max) {
                mid=(min+max)/2;
                if(dinic(mid))
                    max=mid-1;
                else
                    min=mid+1;
              }
              printf("%d ",min);
          }
    return 0;
    }
  • 相关阅读:
    【EXCEL】乱数関数集合
    PHP 获取当前时间前52周 12个月 4个季度
    python 清理没有过期时间的redis
    yii2 使用mongo查询(包含like查询)
    crontab 时间详解
    安装 cronsun
    php的加密&解密 (压缩数据) gzcompress & gzuncompress
    三数之和
    贪心算法解决集合覆盖问题
    KMP算法实现字符串匹配
  • 原文地址:https://www.cnblogs.com/thefirstfeeling/p/4410805.html
Copyright © 2011-2022 走看看