本文整理汇总了C++中Dinic类的典型用法代码示例。如果您正苦于以下问题:C++ Dinic类的具体用法?C++ Dinic怎么用?C++ Dinic使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Dinic类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main()
{
int n, m;
while (cin >> n >> m, n && m) {
Dinic<int, 501> dinic;
vector<pair<int, int> > e;
for (int i=0; i<m; i++) {
int a, b, c;
cin >> a >> b >> c;
e.push_back(make_pair(a, b));
dinic.add_multi(a, b, c);
}
dinic.exec(1, 2);
for (int i=0; i<m; i++) {
if ((dinic.level[e[i].first] != -1 && dinic.level[e[i].second] == -1) ||
(dinic.level[e[i].first] == -1 && dinic.level[e[i].second] != -1)) {
cout << e[i].first << " " << e[i].second << endl;
}
}
cout << endl;
}
return 0;
}
示例2: main
int main()
{
int i,j,count;
while(scanf("%d",&n) != EOF)
{
fun();
Dinic flow;
for(i=1 ; i <= n ; ++i){
if(dp[i]==1) flow.insert(0,i,1);
if(dp[i]==len) flow.insert(i,n+1,1);
for(j=i+1 ; j <= n ; ++j)
if(a[j] > a[i])
flow.insert(i,j,1);
}
printf("%d\n%d\n",len,flow.MaxFlow(0,n+1));
}
return 0;
}
示例3: main
int main() {
scanf("%d%d%d%d", &N, &M, &C1, &C2);
Dinic dinic;
dinic.addEdge(2*C1, 2*C1+1, Dinic::inf);
dinic.addEdge(2*C2, 2*C2+1, Dinic::inf);
for (int i = 1; i <= M; i++) {
int u, v;
scanf("%d%d", &u, &v);
dinic.addEdge(2*u+1, 2*v, Dinic::inf);
dinic.addEdge(2*v+1, 2*u, Dinic::inf);
}
for (int i = 1; i <= N; i++) {
if (i != C1 && i != C2) {
dinic.addEdge(2*i, 2*i+1, 1);
}
}
printf("%d\n", dinic.dinic(2*C1, 2*C2+1));
return 0;
}
示例4: main
int main() {
int N, K;
scanf("%d%d", &N, &K);
Dinic dinic;
const int S = 0, T = 2*N + 1;
for (int i = 1; i <= N; i++) {
dinic.addEdge(S, i, 1);
}
for (int i = N+1; i <= 2*N; i++) {
dinic.addEdge(i, T, 1);
}
for (int i = 0; i < K; i++) {
int r, c;
scanf("%d%d", &r, &c);
dinic.addEdge(r, N+c, Dinic::inf);
}
printf("%d\n", dinic.dinic(S, T));
return 0;
}
示例5: buildgraph
void buildgraph()
{
for(int i=1;i<=M;i++)
scanf("%d",&pig_num[i]);
S=0;T=N+1;Num=T+1;
g.ClearAll(Num);
memset(hash,0,sizeof(hash));
for(int i=1;i<=N;i++)
{
int Qnum;
scanf("%d",&Qnum);
for(int j=1;j<=Qnum;j++)
{
int a;
scanf("%d",&a);
if(hash[a]==0)
{
g.AddEdge(S,i,pig_num[a]);
hash[a]=i;
}else
{
g.AddEdge(hash[a],i,INF);
}
}
scanf("%d",&people_need[i]);
}
for(int i=1;i<=N;i++)
g.AddEdge(i,T,people_need[i]);
}
示例6: dfs
void dfs(Node *p,int flag)
{
if(flag)
{
if(p->ln[0])
{
g.addedge(p->ln[0]-su.rt+pre.cnt,p-su.rt+pre.cnt,p->ln[0]->cost);
dfs(p->ln[0],1);
}
if(p->ln[1])
{
g.addedge(p->ln[1]-su.rt+pre.cnt,p-su.rt+pre.cnt,p->ln[1]->cost);
dfs(p->ln[1],1);
}
}
else
{
if(p->ln[0])
{
g.addedge(p-pre.rt,p->ln[0]-pre.rt,p->ln[0]->cost);
dfs(p->ln[0],0);
}
if(p->ln[1])
{
g.addedge(p-pre.rt,p->ln[1]-pre.rt,p->ln[1]->cost);
dfs(p->ln[1],0);
}
}
}
示例7: solve
void solve(void)
{
int totalp = 0;
for(int i=0;i<n;++i) totalp += ni[i];
for(int i=0;i<n;++i)
for(int j=0;j<n;++j)
if(hypot(x[i]-x[j],y[i]-y[j])<=D)
mat[i][j] = 1;
else
mat[i][j] = 0;
vector<int> ans;
for(int zz=0;zz<n;++zz) {
dinic.init(n*2+1);
for(int i=0;i<n;++i)
dinic.addedge(i,i+n,mi[i]);
for(int i=0;i<n;++i)
dinic.addedge(n*2,i,ni[i]);
for(int i=0;i<n;++i)
for(int j=0;j<n;++j)
if(mat[i][j])
dinic.addedge(i+n,j,totalp);
if(totalp==dinic.maxFlow(n*2,zz))
ans.push_back(zz);
}
if(ans.empty())
puts("-1");
else {
for(int i=0;i<(int)ans.size();++i) {
if(i) putchar(' ');
printf("%d",ans[i]);
}
puts("");
}
}
示例8: BuildGraph
void BuildGraph(int n, int m) {
S = 0, T = n + n + 1;
net.Init(S + T + 1);
for (int i = 0; i < n; ++i) {
int c = CountBit(i);
if (c & 1) {
net.AddEdge(S, i + 1, Less[i]);
net.AddEdge(i + 1 + n, T, More[i]);
} else {
net.AddEdge(S, i + 1, More[i]);
net.AddEdge(i + 1 + n, T, Less[i]);
}
net.AddEdge(i + 1, i + 1 + n, INF);
for (int j = i + 1; j < n; ++j) {
if (CountBit(i ^ j) == 1) {
if (c & 1) {
net.AddEdge(i + 1, j + 1 + n, u[i] ^ u[j]);
} else {
net.AddEdge(j + 1, i + 1 + n, u[i] ^ u[j]);
}
}
}
}
net.MaxFlow(S, T);
}
示例9: solve
bool solve(void)
{
int flowneed = 0;
for(int i=1;i<=n;++i) {
deg[i] = out[i] - in[i];
if(deg[i]%2!=0) return false;
if(deg[i]>0) dinic.addedge(0,i,deg[i]/2);
else if(deg[i]<0) dinic.addedge(i,n+1,-deg[i]/2);
flowneed += deg[i]>0 ? deg[i]/2 : 0;
}
return flowneed==dinic.maxFlow(0,n+1);
}
示例10: check
bool check(int mid)
{
for(int z=0;z+mid-1<B;++z) {
dinic.init(n+B+2);
for(int i=0;i<n;++i) dinic.addedge(n+B,i,1);
for(int i=0;i<B;++i) dinic.addedge(n+i,n+B+1,cap[i]);
for(int i=z;i<z+mid;++i)
for(int j=0;j<n;++j)
dinic.addedge(j,n+prefer[j][i],1);
if(dinic.maxFlow(n+B,n+B+1)==n) return true;
}
return false;
}
示例11: check
bool check(int mid)
{
source = n1+n2; sink = n1+n2+1;
dinic.init(n1+n2+2);
for(int i=0;i<n1;++i) dinic.addedge(source,i,1);
for(int i=0;i<n2;++i) dinic.addedge(i+n1,sink,cap[i]);
for(int i=0;i<n1;++i)
for(int j=0;j<n2;++j)
if(mat[i][j]<=mid)
dinic.addedge(i,j+n1,1);
dinic.maxFlow(source,sink);
return dinic.totalflow==n1;
}
示例12: input
void input(void)
{
scanf("%d%d",&n,&m);
memset(deg,0,sizeof(deg));
memset(out,0,sizeof(out));
memset(in,0,sizeof(in));
dinic.init(n+2);
for(int i=0;i<m;++i) {
int u,v,di;
scanf("%d%d%d",&u,&v,&di);
++out[u];
++in[v];
if(!di) dinic.addedge(u,v,1);
}
}
示例13: build
void build()
{
for(int i=1; i<=n; i++)
g.addedge(pre.pos[i],su.pos[i]+pre.cnt,INF);
dfs(pre.rt,0);
dfs(su.rt ,1);
}
示例14: input
void input()
{
dinic.init();
src = n+m; sink = src+1;
for (int i=0; i<n; i++) {
dinic.add_edge(src, i, 1);
scanf("%s", line);
while (getchar() != '\n') {
scanf("%d", &x);
dinic.add_edge(i, x+n, 1);
}
}
sink_start = dinic.m;
for (int i=0; i<m; i++) {
dinic.add_edge(n+i, sink, n);
}
sink_end = dinic.m;
}
示例15: check
bool check(int f)
{
for (int i=sink_start; i<sink_end; i+=2) {
dinic.edges[i].cap = f;
}
for (int i=0; i<dinic.m; i++) {
dinic.edges[i].flow = 0;
}
return dinic.maxflow(src, sink) == n;
}