POJ 2455 — Secret Milking Machine 网络流+二分

来源:互联网 发布:手机直接安装linux系统 编辑:程序博客网 时间:2024/05/19 12:15

原题:http://poj.org/problem?id=2455

题意:

有n个点,p条无向路,以及每条路的长度;

找出从1到n的t条不同路径,每条路径上的路不能和其他的重复;

问满足条件的所有边中最大的边权;


思路:

二分长度,再用网络流判断是否>=t;


#include<stdio.h>#include<string.h>#include<queue>#include<vector>#include<iostream>#include<algorithm>#define inf 1e9const int maxn = 2500;using namespace std;int n, p, t;int head[maxn], edgenum, dep[maxn];  struct node   {      int from, to, next, cap;      node(){};      node(int from,int to, int next,int cap):from(from),to(to),next(next),cap(cap){}  }edge[1000000], ban[1000000];  void add(int u, int v, int cap)  {      edge[edgenum] = node(u, v, head[u], cap);      head[u] = edgenum++;      edge[edgenum] = node(v, u, head[v], 0);      head[v] = edgenum++;  }  bool bfs(int s,int t)  {      int que[maxn], front = 0, rear = 0;      memset(dep, -1, sizeof(dep));      dep[s] = 0;    que[rear++] = s;      while(front!=rear)      {          int u = que[front++];front%=maxn;          for(int i = head[u];i!=-1;i = edge[i].next)          {              int v = edge[i].to;              if(edge[i].cap>0&&dep[v] == -1)              {                  dep[v] = dep[u]+1;                  que[rear++] = v;                  rear%=maxn;                  if(v == t)return 1;              }          }      }      return 0;  }    int dinic(int s, int t)  {      int res = 0;      while(bfs(s, t))      {      int Stack[maxn], top, cur[maxn];          memcpy(cur, head, sizeof(head));          top = 0;          int u = s;          while(1)          {              if(t == u)              {                 <span style="white-space:pre"></span>int min = inf;                  int loc;                  for(int i = 0;i<top;i++)<span style="white-space:pre"></span>{                      if(min>edge[Stack[i]].cap)                     {               min = edge[Stack[i]].cap;                     loc = i;                     }                 }               for(int i = 0;i<top;i++)                 {                    edge[Stack[i]].cap-=min;                    edge[Stack[i]^1].cap+=min;                 }                 res+=min;                 top = loc;                 u = edge[Stack[top]].from;              }              for(int i = cur[u];i!=-1;cur[u] = i = edge[i].next)            {               if(dep[edge[i].to] == dep[u]+1 && edge[i].cap>0)<span style="white-space:pre"></span>break;    }            if(cur[u]!=-1)              {                 Stack[top++] = cur[u];                 u = edge[cur[u]].to;              }              else               {                 if(top == 0)       break;                 dep[u] = -1;                 u = edge[Stack[--top]].from;              }          }      }      return res;  }  int main(){while(scanf("%d%d%d", &n, &p, &t)!=EOF){int l = inf, r = -inf, ans = inf;for(int i = 1;i<=p;i++){scanf("%d%d%d", &ban[i].from, &ban[i].to, &ban[i].cap);r = max(r, ban[i].cap);l = min(l, ban[i].cap);}while(l<=r){memset(head, -1, sizeof(head));edgenum = 0;int mid = (l+r)/2;for(int i = 1;i<=p;i++){if(ban[i].cap<=mid){add(ban[i].from, ban[i].to, 1);add(ban[i].to, ban[i].from, 1);}}if(dinic(1, n)>=t){ans = min(ans, mid);r = mid-1;}elsel = mid+1;}printf("%d\n", ans);}return 0;}


0 0
原创粉丝点击