当前位置: 代码迷 >> 综合 >> POJ 2455 Secret Milking Machine 二分+最大流 ISAP + GAP优化
  详细解决方案

POJ 2455 Secret Milking Machine 二分+最大流 ISAP + GAP优化

热度:36   发布时间:2024-01-13 18:06:34.0

题目要求如下

一个无向图,有一些双向边。起点为1,目标点为n。要求必须有t条不同的路径。所谓的不同的路径,是一条边只能属于一个路径。

现在求所有路径的最大边 最小的长度是多少。

脑海中顿时闪过二分+最大流,找出模板秒之,因为还没到那种闭目写网络流的程度。 这次的模板是ISAP,我将一个大牛写的指针版本的邻接表改成了数组版本的。

然后还有一个技巧是二分的上下界最好用所有边长度的上下界,因为可能有恶心数据卡时限。

我用了输入优化后207ms,没用之前250ms


/*
ID: sdj22251
PROG: subset
LANG: C++
*/
#include <iostream>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cmath>
#include <ctime>
#define LOCA
#define MAXN 222
#define MAXM 164444
#define INF 100000000
#define eps 1e-7
using namespace std;
struct node
{int ver;    // vertexint cap;    // capacityint flow;   // current flow in this arcint next, rev;
}edge[MAXM];
int dist[MAXN], numbs[MAXN], src, des, n;
int head[MAXN], e;
int xx[MAXM], yy[MAXM], cc[MAXM];
void add(int x, int y, int c)
{       //e记录边的总数edge[e].ver = y;edge[e].cap = c;edge[e].flow = 0;edge[e].rev = e + 1;        //反向边在edge中的下标位置edge[e].next = head[x];   //记录以x为起点的上一条边在edge中的下标位置head[x] = e++;           //以x为起点的边的位置//反向边edge[e].ver = x;edge[e].cap = 0;  //反向边的初始网络流为0edge[e].flow = 0;edge[e].rev = e - 1;edge[e].next = head[y];head[y] = e++;
}
void rev_BFS()
{int Q[MAXN], qhead = 0, qtail = 0;for(int i = 1; i <= n; ++i){dist[i] = MAXN;numbs[i] = 0;}Q[qtail++] = des;dist[des] = 0;numbs[0] = 1;while(qhead != qtail){int v = Q[qhead++];for(int i = head[v]; i != -1; i = edge[i].next){if(edge[edge[i].rev].cap == 0 || dist[edge[i].ver] < MAXN)continue;dist[edge[i].ver] = dist[v] + 1;++numbs[dist[edge[i].ver]];Q[qtail++] = edge[i].ver;}}
}int maxflow()
{int u, totalflow = 0;int Curhead[MAXN], revpath[MAXN];for(int i = 1; i <= n; ++i)Curhead[i] = head[i];u = src;while(dist[src] < n){if(u == des)     // find an augmenting path{int augflow = INF;for(int i = src; i != des; i = edge[Curhead[i]].ver)augflow = min(augflow, edge[Curhead[i]].cap);for(int i = src; i != des; i = edge[Curhead[i]].ver){edge[Curhead[i]].cap -= augflow;edge[edge[Curhead[i]].rev].cap += augflow;edge[Curhead[i]].flow += augflow;edge[edge[Curhead[i]].rev].flow -= augflow;}totalflow += augflow;u = src;}int i;for(i = Curhead[u]; i != -1; i = edge[i].next)if(edge[i].cap > 0 && dist[u] == dist[edge[i].ver] + 1)break;if(i != -1)     // find an admissible arc, then Advance{Curhead[u] = i;revpath[edge[i].ver] = edge[i].rev;u = edge[i].ver;}else        // no admissible arc, then relabel this vertex{if(0 == (--numbs[dist[u]]))break;    // GAP cut, Important!Curhead[u] = head[u];int mindist = n;for(int j = head[u]; j != -1; j = edge[j].next)if(edge[j].cap > 0)mindist = min(mindist, dist[edge[j].ver]);dist[u] = mindist + 1;++numbs[dist[u]];if(u != src)u = edge[revpath[u]].ver;    // Backtrack}}return totalflow;
}
int in()
{int flag = 1;char ch;int a = 0;while((ch = getchar()) == ' ' || ch == '\n');if(ch == '-') flag = -1;elsea += ch - '0';while((ch = getchar()) != ' ' && ch != '\n'){a *= 10;a += ch - '0';}return flag * a;
}
int main()
{int p, t;while(scanf("%d%d%d", &n, &p, &t) != EOF){int mi = INF, mx = 0;for(int i = 1; i <= p; i++){xx[i] = in();yy[i] = in();cc[i] = in();mi = min(mi, cc[i]);mx = max(mx, cc[i]);}int low = mi, high = mx, ans = mx;while(low <= high){int mid = (low + high) >> 1;e = 0;memset(head, -1, sizeof(head));memset(dist, 0, sizeof(dist));memset(numbs, 0, sizeof(numbs));src = 1;des = n;for(int i = 1; i <= p; i++){if(cc[i] <= mid){add(xx[i], yy[i], 1);add(yy[i], xx[i], 1);}}rev_BFS();int tmp = maxflow();//printf("ss %d\n", tmp);if(tmp >= t) { ans = min(ans, mid); high = mid - 1;}else low = mid + 1;}printf("%d\n", ans);}return 0;
}


  相关解决方案