loj2042 「CQOI2016」不同的最小割
分治+最小割
看到题解的第一句话是这个就秒懂了,然后乱七八糟的错误、越界、RE……
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <set>
using namespace std;
int n, m, uu, vv, ww, hea[855], cnt, a[855], ss, tt, lev[855], tmp[855], cur[855];
int maxFlow;
const int oo=0x3f3f3f3f;
queue<int> d;
set<int> se;
struct Edge{
int too, nxt, val;
}edge[17005];
void add_edge(int fro, int too, int val){
edge[cnt].nxt = hea[fro];
edge[cnt].too = too;
edge[cnt].val = val;
hea[fro] = cnt++;
}
bool bfs(){
memset(lev, 0, sizeof(lev));
lev[ss] = 1;
d.push(ss);
while(!d.empty()){
int x=d.front();
d.pop();
for(int i=hea[x]; i!=-1; i=edge[i].nxt){
int t=edge[i].too;
if(!lev[t] && edge[i].val>0){
lev[t] = lev[x] + 1;
d.push(t);
}
}
}
return lev[tt]!=0;
}
int dfs(int x, int lim){
if(x==tt) return lim;
int addFlow=0;
for(int &i=cur[x]; i!=-1; i=edge[i].nxt){
int t=edge[i].too;
if(lev[t]==lev[x]+1 && edge[i].val>0){
int tmp=dfs(t, min(lim-addFlow, edge[i].val));
edge[i].val -= tmp;
edge[i^1].val += tmp;
addFlow += tmp;
if(addFlow>=lim) break;
}
}
return addFlow;
}
void dinic(){
maxFlow = 0;
while(bfs()){
for(int i=1; i<=n; i++)
cur[i] = hea[i];
maxFlow += dfs(ss, oo);
}
}
void solve(int l, int r){
if(l>=r) return ;
for(int i=0; i<cnt; i+=2)
edge[i].val = edge[i^1].val = (edge[i].val + edge[i^1].val) >> 1;
ss = a[l]; tt = a[r];
dinic();
se.insert(maxFlow);
int pu=l, pv=r;
for(int i=l; i<=r; i++)
if(lev[a[i]])
tmp[pu++] = a[i];
else
tmp[pv--] = a[i];
for(int i=l; i<=r; i++)
a[i] = tmp[i];
solve(l, pu-1);
solve(pv+1, r);
}
int main(){
memset(hea, -1, sizeof(hea));
cin>>n>>m;
for(int i=1; i<=m; i++){
scanf("%d %d %d", &uu, &vv, &ww);
add_edge(uu, vv, ww);
add_edge(vv, uu, ww);
}
for(int i=1; i<=n; i++)
a[i] = i;
solve(1, n);
cout<<se.size()<<endl;
return 0;
}
拙いものと思えども、
その手に握る其れこそが、
いつか幻想を生んでいく。