题目链接:https://cn.vjudge.net/contest/68128#problem/A

反思:注意拆点,否则的话节点就没用了,还有注意源点和汇点的赋值。

AC代码:

#include<iostream>
#include<string>
#include<cstring>
#include<iomanip>
#include<stack>
#include<queue>
#include<stdio.h>
#include<algorithm>
using namespace std;
# define inf 0x3f3f3f3f
# define maxn 100+10
# define ll long long
int p,n;
int ori[maxn][maxn],nex[maxn][maxn];
int pre[maxn],vis[maxn];
int u[maxn],v[maxn],w[maxn];
struct node
{
int cost;
int in[maxn];
int out[maxn];
} a[maxn];
void init()
{
memset(ori,0,sizeof(ori));
memset(nex,0,sizeof(nex));
a[1].cost=inf;
for(int i=1; i<=p; i++)
{
a[1].in[i]=0;
a[1].out[i]=0;
a[n+2].in[i]=1;
a[n+2].out[i]=1;
}
a[n+2].cost=inf;
}
bool judge(int t1,int t2)
{
for(int i=1; i<=p; i++)
{
if(a[t1].out[i]!=a[t2].in[i]&&a[t2].in[i]!=2)return false;//多也不行,少也不行。
}
return true;
}
bool bfs(int st,int ed)
{
queue<int>q;
memset(vis,0,sizeof(vis));
vis[st]=1;
q.push(st);
pre[st]=st;
while(!q.empty())
{
int top=q.front();
q.pop();
for(int i=1; i<=2*n; i++)
{
if(vis[i]==0&&nex[top][i])
{
vis[i]=1;
pre[i]=top;
if(i==ed)return true;
q.push(i);
}
}
}
return false;
}
int EK(int st,int ed)
{
int ans=0;
while(bfs(st,ed))
{
int minn=inf ;
for(int i=ed; i!=st; i=pre[i])
{
minn=min(minn,nex[pre[i]][i]);
}
for(int i=ed; i!=st; i=pre[i])
{
nex[pre[i]][i]-=minn;
nex[i][pre[i]]+=minn;
}
ans+=minn;
}
return ans;
}
int main()
{
while(~scanf("%d%d",&p,&n))
{
init();
for(int i=2; i<=n+1; i++)//从2开始是为了把1当做源点
{
scanf("%d",&a[i].cost);
for(int j=1; j<=p; j++)
{
scanf("%d",&a[i].in[j]);
}
for(int j=1; j<=p; j++)
{
scanf("%d",&a[i].out[j]);
}
}
n+=2;//和原来相比加入了源点和汇点
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n; j++)
{
if(i==j)
{
ori[i][j+n]=nex[i][j+n]=a[i].cost;
}
else if(i!=j&&judge(i,j))
{
ori[i+n][j]=nex[i+n][j]=a[i].cost;
}
}
}
int ans=EK(1,n*2);
int num=0;
for(int i=2; i<n; i++)
{
for(int j=2; j<n; j++)
{
if(ori[i+n][j]>nex[i+n][j])//如果拆点后当前流的流量变小了,说明这条路上有流量。
{
u[++num]=i;
v[num]=j;
w[num]=ori[i+n][j]-nex[i+n][j];
}
}
}
printf("%d %d\n",ans,num);
for(int i=1; i<=num; i++)
{
printf("%d %d %d\n",u[i]-1,v[i]-1,w[i]);
}
}
return 0;
}

A - ACM Computer Factory(网络流)的更多相关文章

  1. POJ 3436:ACM Computer Factory 网络流

    ACM Computer Factory Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6247   Accepted: 2 ...

  2. POJ - 3436 ACM Computer Factory 网络流

    POJ-3436:http://poj.org/problem?id=3436 题意 组配计算机,每个机器的能力为x,只能处理一定条件的计算机,能输出特定的计算机配置.进去的要求有1,进来的计算机这个 ...

  3. POJ 3436 ACM Computer Factory (网络流,最大流)

    POJ 3436 ACM Computer Factory (网络流,最大流) Description As you know, all the computers used for ACM cont ...

  4. POJ-3436 ACM Computer Factory(网络流EK)

    As you know, all the computers used for ACM contests must be identical, so the participants compete ...

  5. A - ACM Computer Factory POJ - 3436 网络流

    A - ACM Computer Factory POJ - 3436 As you know, all the computers used for ACM contests must be ide ...

  6. POJ3436 ACM Computer Factory(最大流/Dinic)题解

    ACM Computer Factory Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8944   Accepted: 3 ...

  7. POJ 3464 ACM Computer Factory

    ACM Computer Factory Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4829 Accepted: 1641 ...

  8. ACM Computer Factory(dinic)

    ACM Computer Factory Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5596   Accepted: 1 ...

  9. ACM Computer Factory

    ACM Computer Factory Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6104 Accepted: 2113 ...

随机推荐

  1. 使用Shell脚本删除/清空日志文件

    话不多少,直接上代码: #!/bin/bash workdir=("/home/Tax_Punish_Ret/log_txt") #可填写多个路径, 用空格隔开 # 查找日志文件 ...

  2. L2 L3 L4

    第二层交换机,是根据第二层数据链路层的MAC地址和通过站表选择路由来完成端到端的数据交换的.因为站表的建立与维护是由交换机自动完成,而路由器又是属于第三层设备,其寻址过程是根据IP地址寻址和通过路由表 ...

  3. 多线程-Thread的run()与start()的区别

    总结: 1) start: 用start方法来启动线程,真正实现了多线程运行,这时无需等待run方法体代码执行完毕而直接继续执行下面的代码.通过调用Thread类的start()方法来启动一个线程,这 ...

  4. 是否升级IOS11?IOS11不支持32位程序 查看手机哪些APP不支持

    查看苹果32位APP具体步骤:设置-通用-关于本机-应用程序.如果手机中下载了32位应用的话,苹果会给出应用兼容性提醒:如果手机里没有安装32位应用,右侧没有小三角,点击“应用程序”也会没有反应. I ...

  5. 【刷题】LOJ 6000 「网络流 24 题」搭配飞行员

    题目描述 飞行大队有若干个来自各地的驾驶员,专门驾驶一种型号的飞机,这种飞机每架有两个驾驶员,需一个正驾驶员和一个副驾驶员.由于种种原因,例如相互配合的问题,有些驾驶员不能在同一架飞机上飞行,问如何搭 ...

  6. 消除unchecked cast Warning

    在Java中,经常会将一个Object类型转成自己想要的Map.List等等.通常的做法是: Object obj = ....; Map<String, String> castMap ...

  7. 一些说明&其他奇奇怪怪的东西

    NOIP考完了,这篇博客彻底咕了.

  8. Codeforces 148D 一袋老鼠 Bag of mice | 概率DP 水题

    除非特别忙,我接下来会尽可能翻译我做的每道CF题的题面! Codeforces 148D 一袋老鼠 Bag of mice | 概率DP 水题 题面 胡小兔和司公子都认为对方是垃圾. 为了决出谁才是垃 ...

  9. Jump Game - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Jump Game - LeetCode 注意点 解法 解法一:贪心算法,只关注能到达最远距离,如果能到达的最远距离大于结尾说明能到达,否则不能.并且如果 ...

  10. android:shape的使用(+圆角ListView)(转)

    http://dev.10086.cn/cmdn/wiki/index.php?doc-view-6087.html Android中常常使用shape来定义控件的一些显示属性,今天看了一些shape ...