(网络流) Island Transport --Hdu -- 4280

链接:

http://acm.hdu.edu.cn/showproblem.php?pid=4280

源点是West, 汇点是East, 用Dinic带入求就好了

代码:要用c++提交

#pragma comment(linker, "/STACK:102400000,102400000")  ///手动开大栈区
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std; #define N 100005
#define INF 0x3fffffff struct node {int v, flow, next;} a[N<<]; int Layer[N], Head[N], cnt, n, m; void Init()
{
cnt = ;
memset(Head, -, sizeof(Head));
} void Add(int u, int v, int flow)
{
a[cnt].v = v;
a[cnt].flow = flow;
a[cnt].next = Head[u];
Head[u] = cnt++;
} bool BFS(int Start, int End)
{
queue<int>Q;
Q.push(Start); memset(Layer, , sizeof(Layer));
Layer[Start] = ; while(Q.size())
{
int u = Q.front(); Q.pop(); if(u==End) return true; for(int i=Head[u]; i!=-; i=a[i].next)
{
int v = a[i].v; if(!Layer[v] && a[i].flow)
{
Layer[v] = Layer[u] + ;
Q.push(v);
}
}
}
return false;
}
int DFS(int u, int MaxFlow, int End)
{
if(u==End) return MaxFlow; int uflow=; for(int i=Head[u]; i!=-; i=a[i].next)
{
int v = a[i].v;
if(Layer[v]==Layer[u]+ && a[i].flow)
{
int flow = min(a[i].flow, MaxFlow-uflow);
flow = DFS(v, flow, End); a[i].flow -= flow;
a[i^].flow += flow;
uflow += flow; if(uflow==MaxFlow) break;
}
} if(uflow==) Layer[u] = ; return uflow;
}
int Dinic(int Start, int End)
{
int MaxFlow=; while(BFS(Start, End)==true)
MaxFlow += DFS(Start, INF, End); return MaxFlow;
} int main()
{
int t;
scanf("%d", &t);
while(t--)
{
int i, u, v, flow, East=-INF, West=INF, Start, End, x, y; scanf("%d%d", &n, &m); Init();
for(i=; i<=n; i++)
{
scanf("%d%d", &x, &y);
if(West > x)
{
West = x;
Start = i;
}
if(East < x)
{
East = x;
End = i;
}
} for(i=; i<=m; i++)
{
scanf("%d%d%d", &u, &v, &flow);
Add(u, v, flow);
Add(v, u, flow);
} printf("%d\n", Dinic(Start, End));
}
return ;
}
上一篇:2021-03-26 需要microsoft.NET Framework 3.5 Service Pack


下一篇:Centos 6.5 部署 redmine 3.3