SCU 4442 Party
二分图的最小点权覆盖。
非常感谢巨巨@islands_的解答,还帮我画了一个图。
题目保证给出的边构成的图是一个二分图。
如果没有第三种类型的$frog$,那么问题就很简单了。即选择哪些点,覆盖住所有的边,并且要求选择的点的权值之和最小。可以转换成网络流来解决。
现在有第三种类型的$frog$,可以把这种$frog$拆成两个点,两点之间连边,然后其余和他有矛盾的点,分别向两个点中的一个点连边。画画图可以发现拆点之后的新图也是二分图。对这个图跑一次二分图的最小点权覆盖,如果拆点的边两端有一个点被选择,说明这个$frog$实际上是不选择的,如果拆点的边两端有两个点被选择,说明这个$frog$实际上被选择了,那么只要在结果上减去第三种类型的$frog$的权值和就可以了。
#include<bits/stdc++.h>
using namespace std; int n,m;
int w[],p[],dis[];
vector<int>g[]; const int maxn = + ;
const int INF = 0x7FFFFFFF;
struct Edge
{
int from, to, cap, flow;
Edge(int u, int v, int c, int f) :from(u), to(v), cap(c), flow(f){}
};
vector<Edge>edges;
vector<int>G[maxn];
bool vis[maxn];
int d[maxn];
int cur[maxn];
int s, t; void init()
{
for (int i = ; i < maxn; i++) G[i].clear();
edges.clear();
}
void AddEdge(int from, int to, int cap)
{
edges.push_back(Edge(from, to, cap, ));
edges.push_back(Edge(to, from, , ));
int w = edges.size();
G[from].push_back(w - );
G[to].push_back(w - );
}
bool BFS()
{
memset(vis, , sizeof(vis));
queue<int>Q;
Q.push(s);
d[s] = ;
vis[s] = ;
while (!Q.empty())
{
int x = Q.front();
Q.pop();
for (int i = ; i<G[x].size(); i++)
{
Edge e = edges[G[x][i]];
if (!vis[e.to] && e.cap>e.flow)
{
vis[e.to] = ;
d[e.to] = d[x] + ;
Q.push(e.to);
}
}
}
return vis[t];
}
int DFS(int x, int a)
{
if (x == t || a == )
return a;
int flow = , f;
for (int &i = cur[x]; i<G[x].size(); i++)
{
Edge e = edges[G[x][i]];
if (d[x]+ == d[e.to]&&(f=DFS(e.to,min(a,e.cap-e.flow)))>)
{
edges[G[x][i]].flow+=f;
edges[G[x][i] ^ ].flow-=f;
flow+=f;
a-=f;
if(a==) break;
}
}
if(!flow) d[x] = -;
return flow;
}
int dinic(int s, int t)
{
int flow = ;
while (BFS())
{
memset(cur, , sizeof(cur));
flow += DFS(s, INF);
}
return flow;
} void B(int x)
{
queue<int>Q; Q.push(x); dis[x]=; while(!Q.empty())
{
x = Q.front(); Q.pop();
for(int i=;i<g[x].size();i++)
{
int to = g[x][i];
if(dis[to]!=-) continue;
dis[to] = dis[x]^;
Q.push(to);
}
}
} void ADD(int a,int b)
{
g[a].push_back(b);
g[b].push_back(a);
} int main()
{
while(~scanf("%d%d",&n,&m))
{
for(int i=;i<=*n;i++) g[i].clear(); for(int i=;i<=n;i++) scanf("%d",&w[i]);
for(int i=;i<=n;i++)
{
scanf("%d",&p[i]);
if(p[i]==) ADD(i,i+n);
} for(int i=;i<=m;i++)
{
int A,B; scanf("%d%d",&A,&B);
if(p[A]+p[B]==) continue; if(p[A]==||p[A]==)
{
if(p[B]==||p[B]==) ADD(A,B);
else
{
if(p[A]==) ADD(A,B);
else ADD(A,B+n);
}
} else if(p[A]==)
{
if(p[B]==||p[B]==)
{
if(p[B]==) ADD(A,B);
else ADD(A+n,B);
}
else if(p[B]==)
{
ADD(A,B);
ADD(A+n,B+n);
}
}
} memset(dis,-,sizeof dis);
for(int i=;i<=*n;i++)
{
if(dis[i]!=-) continue;
B(i);
} init(); s=,t=*n+; for(int i=;i<=*n;i++)
{
if(dis[i]==) continue;
for(int j=;j<g[i].size();j++) AddEdge(i,g[i][j],INF);
} for(int i=;i<=*n;i++)
{
int V;
if(i<=n) V = w[i]; else V=w[i-n];
if(dis[i]==) AddEdge(s,i,V);
else AddEdge(i,t,V);
} int ans = dinic(s,t);
for(int i=;i<=n;i++) if(p[i]==) ans=ans-w[i]; printf("%d\n",ans); }
return ;
}
SCU 4442 Party的更多相关文章
- SCU 4442 party 二分图最大点权独立集
每个青蛙喝黑茶或者红茶或者都可以喝 M个矛盾关系 有矛盾的不能喝同种茶 但你可以花费Wi使得这个青蛙消除所有矛盾 把矛盾当作边 青蛙当作点 如果这两个青蛙只喝不同的一种茶就不建边 题目中保证了不存在奇 ...
- scu oj 4442 Party(2015年四川省acm程序设计竞赛)
Party n frogs are invited to a tea party. Frogs are conveniently numbered by 1,2,-,n. The tea party ...
- ACM:SCU 4437 Carries - 水题
SCU 4437 Carries Time Limit:0MS Memory Limit:0KB 64bit IO Format:%lld & %llu Practice ...
- ACM: SCU 4438 Censor - KMP
SCU 4438 Censor Time Limit:0MS Memory Limit:0KB 64bit IO Format:%lld & %llu Practice D ...
- ACM: SCU 4440 Rectangle - 暴力
SCU 4440 Rectangle Time Limit:0MS Memory Limit:0KB 64bit IO Format:%lld & %llu Practic ...
- SCU 4424(求子集排列数)
A - A Time Limit:0MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Practice ...
- SCU 2941 I NEED A OFFER!(01背包变形)
I NEED A OFFER! 64bit IO Format: %lld & %llu Submit Status Description Description Speakless ...
- HDU 4442 Physical Examination
Physical Examination Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64 ...
- SCU 4440 分类: ACM 2015-06-20 23:58 16人阅读 评论(0) 收藏
SCU - 4440 Rectangle Time Limit: Unknown Memory Limit: Unknown 64bit IO Format: %lld & %llu ...
随机推荐
- SpringCloud学习(2)——Rest微服务案例
创建父工程: microservicecloud 创建公共模块api:microservicecloudapi SQL脚本: 此学习路线总共创建3个库, 分别为clouddb01, clouddb0 ...
- expect 批量增加用户及配置密码
start.sh #!/bin/bash password="111111" username="test" if [ $# == 1 ] then usern ...
- [LeetCode] 30. Substring with Concatenation of All Words ☆☆☆
You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...
- 51nod 1449 砝码称重 (进制思想)
1449 砝码称重 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注 现在有好多种砝码,他们的重量是 w0,w1,w ...
- 【CodeForces】713 C. Sonya and Problem Wihtout a Legend
[题目]C. Sonya and Problem Wihtout a Legend [题意]给定n个数字,每次操作可以对一个数字±1,求最少操作次数使数列递增.n<=10^5. [算法]动态规划 ...
- .NET中的异常和异常处理
.NET中的异常(Exception) .net中的中异常的父类是Exception,大多数异常一般继承自Exception. 可以通过编写一个继承自Exception的类的方式,自定义异常类! 异常 ...
- Codeforces Round #441 (Div. 2)
Codeforces Round #441 (Div. 2) A. Trip For Meal 题目描述:给出\(3\)个点,以及任意两个点之间的距离,求从\(1\)个点出发,再走\(n-1\)个点的 ...
- 头像截图上传三种方式之一(一个简单易用的flash插件)(asp.net版本)
flash中有版权声明,不适合商业开发.这是官网地址:http://www.hdfu.net/ 本文参考了http://blog.csdn.net/yafei450225664/article/det ...
- 手机页面或是APP中减少使用setTimeout和setInterval,因为他们会导致页面卡顿
1.setTimeout致使页面的卡顿或是不流畅,打乱模块的生命周期 ,还有setTimeout其实是很难调试的. 当一个页面有众多js代码的时候,setTimeout就是导致页面的卡顿. var s ...
- python从2.6.x升级到2.7.x
[前提] 今日是20171207,目前Linux发行版默认安装的Python版本都是2.6.x,但是这个版本Python已经不再进行维护了. 所以需要将Python做一个升级,到2.7.x [注意] ...