In the vast waters far far away, there are many islands. People are living on the islands, and all the transport among the islands relies on the ships.
You have a transportation company there. Some routes are opened for passengers. Each route is a straight line connecting two different islands, and it is bidirectional. Within an hour, a route can transport a certain number of passengers in one direction. For safety, no two routes are cross or overlap and no routes will pass an island except the departing island and the arriving island. Each island can be treated as a point on the XY plane coordinate system. X coordinate increase from west to east, and Y coordinate increase from south to north.
The transport capacity is important to you. Suppose many passengers depart from the westernmost island and would like to arrive at the easternmost island, the maximum number of passengers arrive at the latter within every hour is the transport capacity. Please calculate it.
Input
The first line contains one integer T (1<=T<=20), the number of test cases.
Then T test cases follow. The first line of each test case contains two integers N and M (2<=N,M<=100000), the number of islands and the number of routes. Islands are number from 1 to N.
Then N lines follow. Each line contain two integers, the X and Y coordinate of an island. The K-th line in the N lines describes the island K. The absolute values of all the coordinates are no more than 100000.
Then M lines follow. Each line contains three integers I1, I2 (1<=I1,I2<=N) and C (1<=C<=10000) . It means there is a route connecting island I1 and island I2, and it can transport C passengers in one direction within an hour.
It is guaranteed that the routes obey the rules described above. There is only one island is westernmost and only one island is easternmost. No two islands would have the same coordinates. Each island can go to any other island by the routes.
Output
For each test case, output an integer in one line, the transport capacity.
题面是一道裸的最大流,给出n个点和m条双向边,点以坐标表示,横坐标最小的是源点,最大的是汇点。按照题目建边跑最大流即可,不过题目里给出的是双向边,所以在建立反向边是要注意容量不是0,而是和正向边一样,然后跑最大流。
不过要注意普通dinic算法会超时,所以这里用的ISAP
#include
#include
#include
#include
#include
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=1e5+5;
const int maxm=1e5+5;
int head[maxn],cur[maxn],dep[maxn],ne,pre[maxn],num[maxn],n,m,S,T,maxflow;
queue q;
struct edge
{
int next,to,cost;
}e[maxm*2];
void add(int u,int v,int w)
{
e[ne].to=v;
e[ne].cost=w;
e[ne].next=head[u];
head[u]=ne++;
e[ne].to=u;
e[ne].cost=w;
e[ne].next=head[v];
head[v]=ne++;
}
void bfs(int t)
{
while(!q.empty()) q.pop();
for(int i=1;i<=n;i++) cur[i]=head[i];
for(int i=1;i<=n;i++) dep[i]=n;
dep[t]=0;
q.push(t);
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u];i!=-1;i=e[i].next)
{
if(dep[e[i].to]==n&&e[i^1].cost)
{
dep[e[i].to]=dep[u]+1;
q.push(e[i].to);
}
}
}
}
int addflow(int s,int t)
{
int ans=INF,u=t;
while(u!=s)
{
ans=min(ans,e[pre[u]].cost);
u=e[pre[u]^1].to;
}
u=t;
while(u!=s)
{
e[pre[u]].cost-=ans;
e[pre[u]^1].cost+=ans;
u=e[pre[u]^1].to;
}
return ans;
}
void ISAP(int s,int t)
{
int u=s;
bfs(t);
for(int i=1;i<=n;i++) num[dep[i]]++;
while(dep[s]is[i].x)
{
minn=is[i].x;
S=i;
}
if(maxx
另外用最高标号预流推进还能再快很多
#include
#include
#include
#include
using std::min;
using std::vector;
using std::queue;
using std::priority_queue;
const int N=2e5+5,M=2e5+5,inf=0x3f3f3f3f;
int n,s,t,tot;
int v[M<<1],w[M<<1],first[N],next[M<<1];
int h[N],e[N],gap[N<<1],inq[N];//节点高度是可以到达2n-1的
void init()
{
tot=0;
memset(first,0,sizeof(first));
memset(h,0,sizeof(h));
memset(e,0,sizeof(e));
memset(gap,0,sizeof(gap));
memset(inq,0,sizeof(inq));
}
struct cmp
{
inline bool operator()(int a,int b) const
{
return h[a] Q;
priority_queue,cmp> pQ;
inline void add_edge(int from,int to,int flow)
{
tot+=2;
v[tot+1]=from;v[tot]=to;w[tot]=flow;w[tot+1]=flow;
next[tot]=first[from];first[from]=tot;
next[tot+1]=first[to];first[to]=tot+1;
return;
}
inline bool bfs()
{
int now;
register int go;
memset(h+1,0x3f,sizeof(int)*n);
h[t]=0;Q.push(t);
while(!Q.empty())
{
now=Q.front();Q.pop();
for(go=first[now];go;go=next[go])
if(w[go^1]&&h[v[go]]>h[now]+1)
h[v[go]]=h[now]+1,Q.push(v[go]);
}
return h[s]!=inf;
}
inline void push(int now)//推送
{
int d;
register int go;
for(go=first[now];go;go=next[go])
if(w[go]&&h[v[go]]+1==h[now])
{
d=min(e[now],w[go]);
w[go]-=d;w[go^1]+=d;e[now]-=d;e[v[go]]+=d;
if(v[go]!=s&&v[go]!=t&&!inq[v[go]])
pQ.push(v[go]),inq[v[go]]=1;
if(!e[now])//已经推送完毕可以直接退出
break;
}
return;
}
inline void relabel(int now)//重贴标签
{
register int go;
h[now]=inf;
for(go=first[now];go;go=next[go])
if(w[go]&&h[v[go]]+1h[now]&&h[i]is[i].x)
{
minn=is[i].x;
s=i;
}
if(maxx