1797: [Ahoi2009]Mincut 最小割
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1893 Solved: 824
[ Submit][ Status][ Discuss]
Description
A,B两个国家正在交战,其中A国的物资运输网中有N个中转站,M条单向道路。设其中第i (1≤i≤M)条道路连接了vi,ui两个中转站,那么中转站vi可以通过该道路到达ui中转站,如果切断这条道路,需要代价ci。现在B国想找出一个路径切断方案,使中转站s不能到达中转站t,并且切断路径的代价之和最小。 小可可一眼就看出,这是一个求最小割的问题。但爱思考的小可可并不局限于此。现在他对每条单向道路提出两个问题: 问题一:是否存在一个最小代价路径切断方案,其中该道路被切断? 问题二:是否对任何一个最小代价路径切断方案,都有该道路被切断? 现在请你回答这两个问题。
Input
第一行有4个正整数,依次为N,M,s和t。第2行到第(M+1)行每行3个正 整数v,u,c表示v中转站到u中转站之间有单向道路相连,单向道路的起点是v, 终点是u,切断它的代价是c(1≤c≤100000)。 注意:两个中转站之间可能有多条道路直接相连。 同一行相邻两数之间可能有一个或多个空格。
Output
对每条单向边,按输入顺序,依次输出一行,包含两个非0即1的整数,分 别表示对问题一和问题二的回答(其中输出1表示是,输出0表示否)。 同一行相邻两数之间用一个空格隔开,每行开头和末尾没有多余空格。
Sample Input
1 2 3
1 3 2
2 4 4
2 5 1
3 5 5
4 6 2
5 6 3
Sample Output
1 0
0 0
1 0
0 0
1 0
1 0
HINT
设第(i+1)行输入的边为i号边,那么{1,2},{6,7},{2,4,6}是仅有的三个最小代价切割方案。它们的并是{1,2,4,6,7},交是 。 【数据规模和约定】 测试数据规模如下表所示 数据编号 N M 数据编号 N M 1 10 50 6 1000 20000 2 20 200 7 1000 40000 3 200 2000 8 2000 50000 4 200 2000 9 3000 60000 5 1000 20000 10 4000 60000
2015.4.16新加数据一组,可能会卡掉从前可以过的程序。
Source
#include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
#include<bitset>
#include<algorithm>
#include<cstring>
#include<map>
#include<stack>
#include<set>
#include<cmath>
#include<ext/pb_ds/priority_queue.hpp>
using namespace std;
const int maxn = 4E3 + 40;
const int maxm = 6E4 + 60;
const int INF = ~0U>>1;
struct E{
int to,cap,flow;
E (int _to = 0,int _cap = 0,int _flow = 0) {to = _to; cap = _cap; flow = _flow;}
}edgs[maxm*2];
int n,m,S,T,cnt,Cnt,scc_num,dfs_clock,cur[maxn],belong[maxn],L[maxn]
,low[maxn],dfn[maxn],vis[maxn],ans1[maxm],ans2[maxm];
vector <int> v[maxn];
queue <int> Q;
stack <int> s;
void Add(int x,int y,int w)
{
v[x].push_back(cnt); edgs[cnt++] = E(y,w,0);
v[y].push_back(cnt); edgs[cnt++] = E(x,0,0);
}
bool BFS()
{
vis[S] = ++Cnt; L[S] = 1; Q.push(S);
while (!Q.empty()) {
int k = Q.front(); Q.pop();
for (int i = 0; i < v[k].size(); i++) {
E e = edgs[v[k][i]];
if (e.cap == e.flow) continue;
if (vis[e.to] == Cnt) continue;
vis[e.to] = Cnt;
L[e.to] = L[k] + 1;
Q.push(e.to);
}
}
return vis[T] == Cnt;
}
int Dicnic(int x,int a)
{
if (x == T || !a) return a;
int flow = 0;
for (int &i = cur[x]; i < v[x].size(); i++) {
E &e = edgs[v[x][i]];
if (e.cap == e.flow) continue;
if (L[e.to] != L[x] + 1) continue;
int f = Dicnic(e.to,min(a,e.cap - e.flow));
if (!f) continue;
e.flow += f;
edgs[v[x][i]^1].flow -= f;
a -= f;
flow += f;
if (!a) return flow;
}
if (!flow) L[x] = -1;
return flow;
}
void DFS(int x)
{
dfn[x] = low[x] = ++dfs_clock;
s.push(x);
for (int i = 0; i < v[x].size(); i++) {
//if (v[x][i]&1) continue;
E e = edgs[v[x][i]];
if (e.cap == e.flow) continue;
if (!dfn[e.to]) DFS(e.to),low[x] = min(low[x],low[e.to]);
else if (!belong[e.to]) low[x] = min(low[x],low[e.to]);
}
if (low[x] >= dfn[x]) {
++scc_num;
for (;;) {
int now = s.top(); s.pop();
belong[now] = scc_num;
if (now == x) break;
}
}
}
int main()
{
#ifdef DMC
freopen("DMC.txt","r",stdin);
#endif
cin >> n >> m >> S >> T;
for (int i = 1; i <= m; i++) {
int x,y,w; scanf("%d%d%d",&x,&y,&w);
Add(x,y,w);
}
int MaxFlow = 0;
while (BFS()) {
for (int i = 1; i <= n; i++) cur[i] = 0;
MaxFlow += Dicnic(S,INF);
}
for (int i = 1; i <= n; i++)
if (!belong[i]) DFS(i);
for (int i = 1; i <= n; i++)
for (int j = 0; j < v[i].size(); j++) {
if (v[i][j] & 1) continue;
int Num = v[i][j]/2 + 1;
E e = edgs[v[i][j]];
if (e.cap != e.flow) continue;
if (belong[i] != belong[e.to]) ans1[Num] = 1;
if (belong[i] == belong[S] && belong[e.to] == belong[T]) ans2[Num] = 1;
}
for (int i = 1; i <= m; i++) printf("%d %d\n",ans1[i],ans2[i]);
return 0;
}