ZOJ 3933 Team Formation
费用流裸题......比赛的时候少写了一句话....导致增加了很多无用的边一直在TLE
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std; const int maxn=+;
int n;
int belong[maxn];
int cost[maxn];
int g[maxn][maxn];
char str[maxn]; const int INF=0x7FFFFFFF;
struct Edge
{
int from,to,cap,flow,cost;
};
int N,M,K,len,s,t;
vector<Edge> edges;
vector<int> G[maxn];
int inq[maxn];
int d[maxn];
int p[maxn];
int a[maxn];
int use[maxn];
int dis[maxn][maxn]; void init()
{
for(int i=; i<maxn; i++) G[i].clear();
edges.clear();
} void Addedge(int from,int to,int cap,int cost)
{
edges.push_back((Edge)
{
from,to,cap,,cost
});
edges.push_back((Edge)
{
to,from,,,-cost
});
len=edges.size();
G[from].push_back(len-);
G[to].push_back(len-);
} bool BellmanFord(int s,int t,int &flow,int &cost)
{ for(int i=; i<maxn; i++) d[i]=INF; memset(inq,,sizeof(inq));
memset(p,-,sizeof(p)); d[s]=;
inq[s]=;
p[s]=;
a[s]=INF; queue<int>Q;
Q.push(s);
while(!Q.empty())
{
int u=Q.front();
Q.pop();
inq[u]=;
for(int i=; i<G[u].size(); i++)
{
Edge& e=edges[G[u][i]];
if(e.cap>e.flow&&d[e.to]>d[u]+e.cost)
{
d[e.to]=d[u]+e.cost;
p[e.to]=G[u][i];
a[e.to]=min(a[u],e.cap-e.flow);
if(!inq[e.to])
{
Q.push(e.to);
inq[e.to]=;
}
}
}
}
if(d[t]==INF) return false;
flow+=a[t];
cost+=d[t]*a[t];
int u=t;
while(u!=s)
{
edges[p[u]].flow+=a[t];
edges[p[u]^].flow-=a[t];
u=edges[p[u]].from;
}
return true;
} void Mincost (int s,int t)
{
int flow=,cost=;
while(BellmanFord(s,t,flow,cost));
printf("%d ",flow);
printf("%d\n",*flow-cost);
} void read()
{
scanf("%s",str);
for(int i=;str[i];i++) belong[i+]=str[i]-'';
scanf("%s",str);
for(int i=;str[i];i++) cost[i+]=str[i]-''; memset(g,,sizeof g); for(int i=;i<=n;i++)
{
int num; scanf("%d",&num);
while(num--)
{
int id; scanf("%d",&id);
g[i][id]=g[id][i]=;
}
}
} int main()
{
int T; scanf("%d",&T);
while(T--)
{
scanf("%d",&n); s=,t=n+;
init();
read();
for(int i=;i<=n;i++)
{
if(belong[i]==) Addedge(,i,,);
else Addedge(i,n+,,);
} for(int i=;i<=n;i++)
{
if(belong[i]==) continue; for(int j=;j<=n;j++)
{
if(i==j) continue;
if(g[i][j]==) continue;
if(belong[j]==) continue;
Addedge(i,j,,cost[i]+cost[j]);
}
}
Mincost(s,t);
for(int i=;i<edges.size();i=i+)
{
if(edges[i].from==) continue;
if(edges[i].to==n+) continue;
if(edges[i].flow==) continue;
printf("%d %d\n",edges[i].from,edges[i].to);
}
}
return ;
}
ZOJ 3933 Team Formation的更多相关文章
- 费用流 ZOJ 3933 Team Formation
题目链接 题意:两个队伍,有一些边相连,问最大组对数以及最多女生数量 分析:费用流模板题,设置两个超级源点和汇点,边的容量为1,费用为男生数量.建边不能重复建边否则会T.zkw费用流在稠密图跑得快,普 ...
- 位运算 ZOJ 3870 Team Formation
题目传送门 /* 题意:找出符合 A^B > max (A, B) 的组数: 位运算:异或的性质,1^1=0, 1^0=1, 0^1=1, 0^0=0:与的性质:1^1=1, 1^0=0, 0^ ...
- Zoj 3870——Team Formation——————【技巧,规律】
Team Formation Time Limit: 3 Seconds Memory Limit: 131072 KB For an upcoming programming contes ...
- ZOJ 3870 Team Formation 贪心二进制
B - Team Formation Description For an upcoming progr ...
- ZOJ 3870 Team Formation 位运算 位异或用与运算做的
For an upcoming programming contest, Edward, the headmaster of Marjar University, is forming a two-m ...
- ZOJ - 3870 Team Formation(异或)
题意:给定N个数,求这N个数中满足A ⊕ B > max{A, B})的AB有多少对.(A,B是N中的某两个数) 分析: 1.异或,首先想到转化为二进制. eg:110011(A)和 1(B)- ...
- zoj The 12th Zhejiang Provincial Collegiate Programming Contest Team Formation
http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5494 The 12th Zhejiang Provincial ...
- ZOJ 3870:Team Formation(位运算&思维)
Team Formation Time Limit: 2 Seconds Memory Limit: 131072 KB For an upcoming programming contest, Ed ...
- 第十二届浙江省大学生程序设计大赛-Team Formation 分类: 比赛 2015-06-26 14:22 50人阅读 评论(0) 收藏
Team Formation Time Limit: 3 Seconds Memory Limit: 131072 KB For an upcoming programming contest, Ed ...
随机推荐
- Git本地项目上传 & SourceTree & GitHub 简单使用
Git(分布式版本控制系统) Git是一款免费.开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目. Git是一个开源的分布式版本控制系统,用以有效.高速的处理从很小到非常大的项目版本管理 ...
- JavaScript(2)——对象属性、原型与原型链
对象属性.原型与原型链 哈哈哈,我的第二篇博客哟,说的是对象属性.原型与原型链.可能这些只是某些小点串联起来的,逻辑性没有很强.所以会对文章的可读性和理解性带来一些困扰.不过,今天我又前进了那么一小步 ...
- js基础之数据类型
一:JavaScript 数据类型:字符串.数字.布尔.数组.对象.Null.Undefined JavaScript 字符串; var carname="Bill Gates ...
- Windows API 之 CreateFile、CreateFileMapping 、MapViewOfFile
CreateFile Creates or opens a file or I/O device. The most commonly used I/O devices are as follows: ...
- spring,hibernate配置事务 beans.xml
beans.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="htt ...
- 中国天气网 JSON接口的城市编码解析及结果
最近在弄一个Android应用,其中一个功能是天气情况展示,准备使用google的天气API服务(http://www.google.com/ig/api?hl=zh-cn&weather=, ...
- Abandoned country
Abandoned country Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- 微信公众号系列 --- ionic在IOS的键盘弹出问题
在使用ionic开发IOS系统微信的时候会有一个苦恼的问题,填写表单的时候键盘会挡住输入框,其实并不算什么大问题,只要用户输入一个字就可以立刻看见输入框了. 可惜的是,有些客户是不讲理的,他才不管这个 ...
- jaxb异常 Class has two properties of the same name username
import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; im ...
- 解决BT5不能使用putty连接问题
root@bt:~# cd /etc/sshroot@bt:/etc/ssh# sshd-generate Generating public/private rsa1 key pair.Your i ...