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 ...
随机推荐
- google 浏览器插件安装
谷歌访问助手
- MT【162】渐近估计
(2017北大优特测试第八题) 数列 \(\{a_n\}\) 满足 \(a_1=1\),\(a_{n+1}=a_n+\dfrac{1}{a_n}\),若 \(a_{2017}\in (k,k+1)\) ...
- 【poj2411】 Mondriaan's Dream
http://poj.org/problem?id=2411 (题目链接) 题意 一个$n*m$的网格,用$1*2$的方块填满有多少种方案. Solution 轮廓线dp板子.按格dp,对上方和左方的 ...
- java线程池赏析
1.线程池的顶级接口(Executor) 线程池的顶级接口(jdk > 1.5).仅仅定义了方法execute(Runnable). 该方法接收一个Runnable实例,用来执行一个任务,该任务 ...
- 列表批量删除和单个删除共用一个方法传递集合到Controller
前台方法(前台部分用的bootstrap): 后台Controller: 这里的id实际就是前台传过来的集合,这种方式,后台的 集合接收变量名称可以随意起名,因为前台传过来的就是一个集合,本身就没有名 ...
- 在前台jsp页面中取得并使用后台放入域中变量的方法
定义成 js变量后,在js中也可以自由使用.
- [转]Multivariate Time Series Forecasting with LSTMs in Keras
1. Air Pollution Forecasting In this tutorial, we are going to use the Air Quality dataset. This is ...
- python的map函数的使用方法详解以及使用案例(处理每个元素的自增、自减、平方等)
1.用我们之前学过的求一下平方(只有一个列表) #求平方 num=[1,5,6,2,7,8] a=[] for n in num: a.append(n**2) print (a) C:\python ...
- 在VC6/VC2005下使程序直接具有XP风格(XP Style):
原文 首先将以下文本保存为XpStyle.mainfest(后经实践,文件名和后缀是什么都无所谓) <?xml version="1.0" encoding="UT ...
- Go_17:GoLang中如何使用多参数属性传参
我们常常因为传入的参数不确定而头疼不已,golang 为我们提供了接入多值参数用于解决这个问题.但是一般我们直接写已知代码即所有的值都知道一个一个塞进去就好了,但是绝大部分我们是得到用户的大量输入想通 ...