A - ACM Computer Factory(网络流)
题目链接: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(网络流)的更多相关文章
- POJ 3436:ACM Computer Factory 网络流
ACM Computer Factory Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6247 Accepted: 2 ...
- POJ - 3436 ACM Computer Factory 网络流
POJ-3436:http://poj.org/problem?id=3436 题意 组配计算机,每个机器的能力为x,只能处理一定条件的计算机,能输出特定的计算机配置.进去的要求有1,进来的计算机这个 ...
- POJ 3436 ACM Computer Factory (网络流,最大流)
POJ 3436 ACM Computer Factory (网络流,最大流) Description As you know, all the computers used for ACM cont ...
- POJ-3436 ACM Computer Factory(网络流EK)
As you know, all the computers used for ACM contests must be identical, so the participants compete ...
- 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 ...
- POJ3436 ACM Computer Factory(最大流/Dinic)题解
ACM Computer Factory Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8944 Accepted: 3 ...
- POJ 3464 ACM Computer Factory
ACM Computer Factory Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4829 Accepted: 1641 ...
- ACM Computer Factory(dinic)
ACM Computer Factory Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5596 Accepted: 1 ...
- ACM Computer Factory
ACM Computer Factory Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6104 Accepted: 2113 ...
随机推荐
- Hadoop 2.6.0 HIVE 2.1.1配置
我用的hadoop 是2.6.0 版本 ,hive 是 2.1.1版本进入:/home/zkpk/apache-hive-2.1.1-bin/执行hive 后报错: (1)Exception in t ...
- BZOJ 3731: Gty的超级妹子树
3731: Gty的超级妹子树 Time Limit: 7 Sec Memory Limit: 32 MBSubmit: 346 Solved: 96[Submit][Status][Discus ...
- BZOJ3155:Preprefix sum——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=3155 最朴素的想法是两棵树状数组,一个记录前缀和,一个记录前缀前缀和,但是第二个我们非常不好修改 ...
- 【BZOJ4804】欧拉心算
Description 给定数字\(n\)(\(n\le 10^7\)),求: \[ \sum_{i=1}^n\sum_{j=1}^n\varphi(\gcd(i,j)) \] 多组数据输入,数据 ...
- 毕业设计预习:maxplus2入门教程
maxplus2入门教程 一.安装配置(maxplus2.zip) 下载安装完成后,运行maxstart.exe,显示如下错误提示: 为节省配置工作,在E:盘下新建maxplus2文件夹,仅将所需附加 ...
- bzoj2758【scoi2012】Blinker的的噩梦
题目描述 一天Blinker醒来,发现自己成为了一个二维世界的点,而且被标记上了一个奇怪的值. 这个世界是由N个边界互不相交(且不相切)的图形组成,这里图形仅包括圆和凸多边形.每个图形还有一个权值.每 ...
- Centos最小化安装后还需要安装的软件包收集
自己也是初学,只是记录一下自己使用的时候需要安装的包: 1. yum install -y make wget mlocate net-tools 2. yum install -y gcc open ...
- html视频背景
视频作为网页背景的限制因素 在动手编码实现前,视频作为网页背景的有些问题我们要先考虑清楚: 并不是因为技术上可行你就可以任意使用:作为背景的视频内容必须能增强页面内容的感染力,而不是因为漂亮或技术上很 ...
- 十、Shell基础
一.shell概述 1.Shell是什么 shell是一个命令行解释器,他为用户提供了一个向linux内核发送请求以便运行程序的界面系统级程序,用户可以用shell来启动.挂起.停止甚至编写一些程序 ...
- Linux应用程序设计之网络基础编程
1.TCP/IP协议概述 1.1.OSI参考模型及TCP/IP参考模型 OSI协议参考模型是基于国际标准化组织(ISO)的建议发展起来的,从上到下工分为7层:应用层,表示层,会话层,传输层,网络层,数 ...