惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Y
Y Combinator Blog
MyScale Blog
MyScale Blog
Recent Announcements
Recent Announcements
酷 壳 – CoolShell
酷 壳 – CoolShell
GbyAI
GbyAI
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
N
Netflix TechBlog - Medium
V
V2EX
MongoDB | Blog
MongoDB | Blog
Microsoft Security Blog
Microsoft Security Blog
博客园 - 三生石上(FineUI控件)
Stack Overflow Blog
Stack Overflow Blog
U
Unit 42
B
Blog
Microsoft Azure Blog
Microsoft Azure Blog
博客园_首页
H
Help Net Security
D
DataBreaches.Net
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
阮一峰的网络日志
阮一峰的网络日志
T
The Blog of Author Tim Ferriss
C
Check Point Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报

博客园 - zy_nic

SRM 424 div1 900 ProductOfPrices 翻译 .. emacs的c++mode contest contest 我的2-sat模板 Solution to GCJ Practice Contest Problem C, Cycles 约瑟夫问题的数学方法 我的模板 图的应用 pku3411 今天的比赛 关于建图的 死老鼠安装成功 pku3141 先帖个题目上来 hnu 11028 hnu 11015 joj 儿死三八
pku 1273
zy_nic · 2007-09-07 · via 博客园 - zy_nic

啥也不说了 最大流  注意重边

 1#include <iostream>
 2using namespace std;
 3
 4//求网络最大流,邻接阵形式
 5//返回最大流量,flow返回每条边的流量
 6//传入网络节点数n,容量mat,源点source,汇点sink
 7
 8#define MAXN 210
 9#define inf 2110000000
10
11int max_flow(int n,int mat[][MAXN],int source,int sink,int flow[][MAXN]){
12    int pre[MAXN],que[MAXN],d[MAXN],p,q,t,i,j;
13    if (source==sink) return inf;
14    for (i=0;i<n;i++)
15        for (j=0;j<n;flow[i][j++]=0);
16    for (;;){
17        for (i=0;i<n;pre[i++]=0);
18        pre[t=source]=source+1,d[t]=inf;
19        for (p=q=0;p<=q&&!pre[sink];t=que[p++])
20            for (i=0;i<n;i++)
21                if (!pre[i]&&(j=mat[t][i]-flow[t][i]))
22                    pre[que[q++]=i]=t+1,d[i]=d[t]<j?d[t]:j;
23                else if (!pre[i]&&(j=flow[i][t]))
24                    pre[que[q++]=i]=-t-1,d[i]=d[t]<j?d[t]:j;
25        if (!pre[sink]) break;
26        for (i=sink;i!=source;)
27            if (pre[i]>0)
28                flow[pre[i]-1][i]+=d[sink],i=pre[i]-1;
29            else
30                flow[i][-pre[i]-1]-=d[sink],i=-pre[i]-1;
31    }

32    for (j=i=0;i<n;j+=flow[source][i++]);
33    return j;
34}

35
36
37int mat[MAXN][MAXN],flow[MAXN][MAXN];
38
39int n,m;
40
41void init()
42{
43    int i;
44    int f,t,c;
45
46    memset(mat,0,sizeof(mat));
47
48    for (i=0;i<m;i++)
49    {
50        scanf("%d%d%d",&f,&t,&c);
51        mat[f-1][t-1]+=c;
52    }

53}

54
55int main()
56{
57    while (scanf("%d%d",&m,&n)==2)
58    {
59        init();
60        printf("%d\n",max_flow(n,mat,0,n-1,flow));
61    }

62    return 0;
63}

64
65