ZOJ 1532 Internship (Dinic)

看来模板又错了,敲你妈妈小饼干

#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<stack>
#include<vector>
#include<map>
using namespace std;
const int inf = 0x3f3f3f3f ;
struct node {
    int u,v,Flow,next;
};

node Li[3000];
int Head[110],top;
int vis[110],ans[110];
bool vis1[110],vis2[110];
int n,m,l;
int s,t;
void Add(int u,int v,int f) {
    Li[top].v=v;
    Li[top].u=u;
    Li[top].Flow=f;
    Li[top].next=Head[u];
    Head[u]=top++;
}
bool BFS() {
    memset(vis,-1,sizeof(vis));
    vis[s]=0;
    queue<int>q;
    q.push(s);
    while(!q.empty()) {
        int u=q.front();
        q.pop();
        for(int i=Head[u]; i!=-1; i=Li[i].next) {
            if(Li[i].Flow&&vis[Li[i].v]==-1) {
                vis[Li[i].v]=vis[u]+1;/**为何要如此标记?改**/
                q.push(Li[i].v);
            }
        }
    }
    return vis[t]!=-1;
}
int DFS(int u,int f) {
    if(u==t) {
        return f;
    }
    int ans=0;
    for(int i=Head[u]; i!=-1; i=Li[i].next) {
        if(Li[i].Flow&&vis[Li[i].v]==vis[u]+1) {
            int d=DFS(Li[i].v,min(f,Li[i].Flow));
            f-=d;
            Li[i].Flow-=d;
            Li[i^1].Flow+=d;//神奇呀!!!!!!
            ans+=d;
        }
    }
    return ans;
}
void dfs(int u,bool *vist,int op) {
    vist[u]=true;
    for(int i=Head[u]; i!=-1; i=Li[i].next) {
        if(!vist[Li[i].v]&&Li[i^op].Flow!=0) {
            dfs(Li[i].v,vist,op);
        }
    }
}
void Dinic() {
    int ans;
    while(BFS()) {
        ans=DFS(s,inf);
    }
}
int main() {
    while(scanf("%d%d%d",&n,&m,&l)!=EOF&&n) {
        s=n+m+1;
        t=0;
        memset(Head,-1,sizeof(Head));
        int a,b,c;
        top = 0;
        for(int i=0; i<l; i++) {
            scanf("%d%d%d",&a,&b,&c);
            Add(a,b,c);
            Add(b,a,0);
        }
        for(int i=1; i<=n; i++) {
            Add(s,i,inf);
            Add(i,s,0);
        }
        Dinic();
        memset(vis1,0,sizeof(vis1));
        memset(vis2,0,sizeof(vis2));
        dfs(s,vis1,0);
        dfs(t,vis2,1);
        int num = 0;
        for(int i=0; i<l; i++) {
            if(Li[i<<1].Flow==0){
                Li[i<<1].Flow=1;
                if(BFS())ans[num++]=i+1;
                Li[i<<1].Flow=0;

            }
        }
        if(num) {
            for(int i=0; i<num; i++) {
                if(i) {
                    printf(" ");
                }
                printf("%d",ans[i]);
            }
        }
        printf("\n");
    }
}

  

原文地址:https://www.cnblogs.com/ZGQblogs/p/9410738.html

时间: 2024-11-11 00:32:17

ZOJ 1532 Internship (Dinic)的相关文章

poj 1459 Power Network (dinic)

Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 23059   Accepted: 12072 Description A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport lines. A node u may be supplied

zoj 1037 Gridland (简单)

Gridland Time Limit: 2 Seconds      Memory Limit: 65536 KB Background For years, computer scientists have been trying to find efficient solutions to different computing problems. For some of them efficient algorithms are already available, these are

zoj 3612 Median (splay)

题目大意: 添加和删除一个数,然后输出中位数. 简单的Splay   维护Splay上有多少个节点就可以了 #include <cstdio> #include <iostream> #define inf 1LL<<60 #define maxn 222222 #define keyTree (ch[ch[root][1]][0]) using namespace std; typedef long long LL; int S[maxn],que[maxn],ch[

POJ1607 &amp; HDU 1330 &amp; ZOJ 1216 Deck(数学题)

题目链接: POJ  1607 : http://poj.org/problem?id=1607 HDU 1330 :http://acm.hdu.edu.cn/showproblem.php?pid=1330 ZOJ  1216 : Description A single playing card can be placed on a table, carefully, so that the short edges of the card are parallel to the table

ZOJ 3890 Wumpus (BFS)

题目链接:ZOJ 3890 Wumpus 题意:一个人在n*n的地图的左下角,他想逃出地图,并且他想要分数达到最高,他可以做的操作有直走,左转,右转,射击,爬,挖金矿6个操作,每个操作后分数减10,但是挖到金矿分数加1000,问你逃出地图的最大分数是多少.他可能遇到怪兽但是他不射击(也就是说没有射击的操作),金库在地图中也只有一个. 思路:BFS搜一遍就好了 AC代码: #include <stdio.h> #include <string.h> #include <queu

18.11.23 POJ 3436 ACM Computer Factory(dinic)

描述 As you know, all the computers used for ACM contests must be identical, so the participants compete on equal terms. That is why all these computers are historically produced at the same factory. Every ACM computer consists of P parts. When all the

zoj 2921 Stock(贪心)

Optiver sponsored problem. After years of hard work Optiver has developed a mathematical model that allows them to predict wether or not a company will be succesful. This obviously gives them a great advantage on the stock market. In the past, Optive

网络流模板(Dinic)

1 #include<iostream> 2 #include<algorithm> 3 #include<cstdio> 4 #include<cstring> 5 #include<cmath> 6 #include<cstdlib> 7 #include<vector> 8 using namespace std; 9 typedef long long ll; 10 typedef long double ld;

HDU 1986 &amp; ZOJ 2989 Encoding(模拟)

题目链接: HDU: http://acm.hdu.edu.cn/showproblem.php?pid=1986 ZOJ: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1988 HDU 1987 & ZOJ 2990 和这题刚好相反,也是比较容易模拟: Chip and Dale have devised an encryption method to hide their (written) text messages