费用流裸题......比赛的时候少写了一句话....导致增加了很多无用的边一直在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的更多相关文章

  1. 费用流 ZOJ 3933 Team Formation

    题目链接 题意:两个队伍,有一些边相连,问最大组对数以及最多女生数量 分析:费用流模板题,设置两个超级源点和汇点,边的容量为1,费用为男生数量.建边不能重复建边否则会T.zkw费用流在稠密图跑得快,普 ...

  2. 位运算 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^ ...

  3. Zoj 3870——Team Formation——————【技巧,规律】

    Team Formation Time Limit: 3 Seconds      Memory Limit: 131072 KB For an upcoming programming contes ...

  4. ZOJ 3870 Team Formation 贪心二进制

                                                    B - Team Formation Description For an upcoming progr ...

  5. ZOJ 3870 Team Formation 位运算 位异或用与运算做的

    For an upcoming programming contest, Edward, the headmaster of Marjar University, is forming a two-m ...

  6. ZOJ - 3870 Team Formation(异或)

    题意:给定N个数,求这N个数中满足A ⊕ B > max{A, B})的AB有多少对.(A,B是N中的某两个数) 分析: 1.异或,首先想到转化为二进制. eg:110011(A)和 1(B)- ...

  7. zoj The 12th Zhejiang Provincial Collegiate Programming Contest Team Formation

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5494 The 12th Zhejiang Provincial ...

  8. ZOJ 3870:Team Formation(位运算&思维)

    Team Formation Time Limit: 2 Seconds Memory Limit: 131072 KB For an upcoming programming contest, Ed ...

  9. 第十二届浙江省大学生程序设计大赛-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 ...

随机推荐

  1. # 泰语字符串字符分割 --- UTF-8编码格式

    1.泰语编码格式 泰语用的编码格式是:ISO 8859-11,这个是Latin编码系列,是从"ISO-8859-1"发展过来的,采用的是8bit一个字,所以泰语中的英文字母或者数字 ...

  2. robot framework -记录错误

    1.注意ie浏览器代理设置,报奇怪的错误 2.注意浏览器的安全设置,保护模式全部不要勾选

  3. gulp相关知识(2)

    将之前的东西的理论部分整理了一下—— gulp 前端构建工具 它可以帮助我们完成一些自动化操作 它需要一些插件的支持 var gulp = require('gulp'); --> gulp命令 ...

  4. Linux学习 -- Shell基础 -- Bash基本功能

    历史命令 history -c   clear -w   写入 ~/.bash_history 默认保存1000条, 可在/etc/profile中修改 调用 Tab补全 命令.目录.文件 命令别名 ...

  5. ViewHolder最简洁的写法

    通用viewHolder工具类: public class ViewHolder { // I added a generic return type to reduce the casting no ...

  6. java在CMD环境下执行需注意字符集设定

    最近有个小工具需要将DMS系统中随机文件名替换为原始文件名,当导出原始文件名到csv文件中,用小 工具读取然后rename时,发现在eclipse环境下运行正常,简繁中文名称也正常:但放到cmd中执行 ...

  7. 【Android Demo】通过WebService获取今日天气情况--转

    因为本身是在搞.NET方面的东东,现在在学习Android,所以想实现Android通过WebService接口来获取数据,网上很多例子还有有问题的.参考:Android 通过WebService进行 ...

  8. CentOS 6 NFS的安装配置

    $ sudo umount /mnt/ /mnt was not found in /proc/mounts /mnt was not found in /proc/mounts 解决: umount ...

  9. linux下提示bash:command not found

    新安装的linux系统,如果进行精简安装可能会出现bash:command not found 的提示,大家在安装的时候可以选择默认安装basic的组件,一般即可.到时候可以再升级.   如果新装的系 ...

  10. webapp之路--之ios上图标

    以前我们用过favicon在浏览器给网站进行身份标识,用法如下: <link href="http://image.feeliu.com/web/favicon.ico" r ...