/*
This Problem beat me completely.
Beat my template completely.
Beat my funtion comletely.
Waste my time comletely.
It's a naked maximum-stream problem.
The main point is your template.
It make me realize that my SAP template is a sh*t!
....To make my SAP faster, I have done three kinds of optimization.
1. Record the adjacency node which has minimum distance;
2. Initailize the distance by using BFS;
3. As I got a Stack-Overflow, I simulate the DFS by using stack in STL;
Then I got AC in 2s+.
Of course, the problem is also correspond to the Minimum-Cut in Dual-Graph.
Hence, Dijkstra with minimum heap is optimal algorithm.
*/
//Header
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <cctype>
#include <queue>
#include <stack>
#include <cmath>
using namespace std;
//Macro
//STL-Alias
#define UN(c, a) unique(c, a)
#define MS(c, a) memset(c, a, sizeof c)
#define FLC(c, a ,b) fill(c, c + a, b)
#define LOS(c, a, b) lower_bound(c, a, b)
#define UPS(c, a, b) upper_bound(c, a, b)
//Syntax-Alias
#define Rep(c, a, b) for (int c = (a); c < (b); c++)
#define Nre(c, a, b) for (int c = (a); c > (b); c--)
//DEBUG
#define FK puts("Fu*k here!")
#define PA(s, c, a, b, p, f){\
printf(s);\
Rep(c, a, b) printf(p, (f));\
puts("");}
//Constant
#define INFL (0x7fffffffffffffffLL)
#define INFI (0x7fffffff)
#define MOD ()
#define MAXN (100005)
//Type-Alias
typedef long long LL;
typedef long double LD;
typedef int AI[MAXN];
typedef bool AB[MAXN];
typedef double AD[MAXN];
typedef LL ALL[MAXN];
typedef LD ALD[MAXN];
//FSG
struct Edge
{
int v, w, next;
};
vector<Edge> E;
AI L;
void G_ini()
{
E.clear();
MS(L, -1);
}
void AE(int u, int v, int w)
{
Edge te = {v, w, L[u]};
L[u] = E.size();
E.push_back(te);
}
#define Ere(c, a, L) for (int c = L[a]; c != -1; c = E[c].next)
//ISAP
int ISAP(int src, int snk, int V)
{
static AI dis, gap, _L, se;
int u = src, mxf = 0, te = 0;
memcpy(_L, L, sizeof L);
MS(dis, -1); MS(gap, 0);
gap[dis[snk] = 0] = 1;
queue<int> Q;
Q.push(snk);
while (!Q.empty())
{
int u = Q.front(); Q.pop();
Ere(i, u, L) if (E[i].w && dis[E[i].v] < 0)
{
gap[dis[E[i].v] = dis[u] + 1]++;
Q.push(E[i].v);
}
}
while (dis[src] < V)
{
int _i = -1;
Ere(i, u, _L) if (E[i].w && dis[u] == dis[E[i].v] + 1)
{
_i = i;
break;
}
if (_i != -1)
{
_L[u] = _i;
u = E[se[te++] = _i].v;
}
else
{
int md = INFI;
_i = -1;
Ere(i, u, L) if (E[i].w && dis[E[i].v] < md)
{
md = dis[E[i].v];
_L[u] = i;
}
if (!--gap[dis[u]]) break;
gap[dis[u] = md + 1]++;
if (u != src) u = E[se[te-- - 1] ^ 1].v;
}
if (u == snk)
{
int _i = 0, mf = INFI;
Rep(i, 0, te) if (E[se[i]].w < mf)
{
mf = E[se[i]].w;
_i = i;
}
Rep(i, 0, te)
{
E[se[i]].w -= mf;
E[se[i] ^ 1].w += mf;
}
mxf += mf;
te = _i;
u = E[se[_i] ^ 1].v;
}
}
return mxf;
}
struct Isl
{
int x, y;
void inp()
{
scanf("%d%d", &x, &y);
}
} I[MAXN];
int n, m;
int main()
{
int T;
scanf("%d", &T);
while (T--)
{
//Initialize
scanf("%d%d", &n, &m);
I[0].inp();
int src = 0, snk = 0;
Rep(i, 1, n)
{
I[i].inp();
if (I[i].x < I[src].x) src = i;
if (I[i].x > I[snk].x) snk = i;
}
G_ini();
while (m--)
{
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
u--;
v--;
AE(u, v, w);
AE(v, u, w);
}
//Solve
printf("%d\n", ISAP(src, snk, n));
}
return 0;
}
hdu 4280 Island Transport
最新推荐文章于 2022-02-25 19:38:17 发布