HDU 4309 Contest 1
最大流建图。开始以为旧桥有1000座,没敢用枚举,后来看看题目发现了只是十二座。枚举桥的状态没问题。
对于隧道的容量W,可以虚拟出第三个结点表示,如u->v。增加一个点p,u->p(INF),p->v(INF),p->End(W);
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring> using namespace std; const int INF=0x3f3f3f3f;
const int MAXN=300;//点数的最大值
const int MAXM=5000;//边数的最大值 struct Node{
int from,to,next;
int cap;
}edge[MAXM]; struct Edge{
int u,v,w;
}Tun[30],Acient[20],Modern[1000]; int tol;
int dep[MAXN];
int head[MAXN];
int val[MAXN]; void init(){
tol=0;
memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int w){
edge[tol].from=u;
edge[tol].to=v; edge[tol].cap=w; edge[tol].next=head[u];
head[u]=tol++;
edge[tol].from=v;
edge[tol].to=u;
edge[tol].cap=0;
edge[tol].next=head[v];
head[v]=tol++;
} int BFS(int start,int end){
int que[MAXN];
int front,rear; front=rear=0;
memset(dep,-1,sizeof(dep));
que[rear++]=start;
dep[start]=0;
while(front!=rear){
int u=que[front++];
if(front==MAXN)front=0;
for(int i= head[u];i!=-1; i=edge[i].next){
int v=edge[i].to;
if(edge[i].cap>0&& dep[v]==-1){
dep[v]=dep[u]+1;
que[rear++]=v;
if(rear>=MAXN) rear=0;
if(v==end)return 1;
}
}
}
return 0;
} int dinic(int start,int end){
int res=0;
int top;
int stack[MAXN];
int cur[MAXN];
while(BFS(start,end)){
memcpy(cur,head, sizeof(head));
int u=start;
top=0;
while(1){
if(u==end){
int min=INF;
int loc;
for(int i=0;i<top;i++)
if(min>edge [stack[i]].cap){
min=edge [stack[i]].cap;
loc=i;
}
for(int i=0;i<top;i++){
edge[stack[i]].cap-=min;
edge[stack[i]^1].cap+=min;
}
res+=min;
top=loc;
u=edge[stack[top]].from;
}
for(int i=cur[u]; i!=-1; cur[u]=i=edge[i].next)
if(edge[i].cap!=0 && dep[u]+1==dep[edge[i].to])
break;
if(cur[u] !=-1){
stack [top++]= cur[u];
u=edge[cur[u]].to;
}
else{
if(top==0) break;
dep[u]=-1;
u= edge[stack [--top] ].from;
}
}
}
return res;
} int main(){
int n,e;
int nT,nA,nM;
int u,v,w,p;
while(scanf("%d%d",&n,&e)!=EOF){
nT=nA=nM=0;
for(int i=1;i<=n;i++)
scanf("%d",&val[i]);
for(int i=0;i<e;i++){
scanf("%d%d%d%d",&u,&v,&w,&p);
if(p==0){
Modern[nM].u=u;Modern[nM].v=v;
Modern[nM].w=w;
nM++;
}
else if(p<0){
Tun[nT].u=u;Tun[nT].v=v;
Tun[nT].w=w;
nT++;
}
else{
Acient[nA].u=u; Acient[nA].v=v;
Acient[nA].w=w;
nA++;
}
}
int Start=0,End;
End=n+nT+1;
int len=(1<<nA);
int cost,people,ansc=0,anspeople=0;
for(int i=0;i<len;i++){
init();
cost=0;
for(int k=1;k<=n;k++){
addedge(Start,k,val[k]);
}
for(int k=0;k<nA;k++){
if(i&(1<<k)){
addedge(Acient[k].u,Acient[k].v,INF);
cost+=Acient[k].w;
}
else
addedge(Acient[k].u,Acient[k].v,1);
}
for(int k=0;k<nM;k++){
addedge(Modern[k].u,Modern[k].v,INF);
}
for(int k=0;k<nT;k++){
addedge(Tun[k].u,n+k+1,INF);
addedge(n+k+1,Tun[k].v,INF);
addedge(n+k+1,End,Tun[k].w);
}
// system("pause");
people=dinic(Start,End);
if(people>anspeople){
anspeople=people;
ansc=cost;
}
else if(people==anspeople){
ansc=min(cost,ansc);
}
}
if(anspeople==0){
printf("Poor Heaven Empire\n");
}
else
printf("%d %d\n",anspeople,ansc);
}
return 0;
}
HDU 4309 Contest 1的更多相关文章
- HDU 4309 Seikimatsu Occult Tonneru
Seikimatsu Occult Tonneru Time Limit: 6000ms Memory Limit: 32768KB This problem will be judged on HD ...
- HDU 4309 Seikimatsu Occult Tonneru(最大流+二进制枚举)
http://acm.hdu.edu.cn/showproblem.php?pid=4309 题意: 有n个城市,每个城市有num[i]个居民,有敌人要进行地毯式轰击,居民们要逃到隧道去.现在有隧道, ...
- HDU 5045 Contest(状压DP)
Problem Description In the ACM International Collegiate Programming Contest, each team consist of th ...
- hdu - 5045 - Contest(国家压缩dp)
意甲冠军:N个人M通过主打歌有自己的期望,每个问题发送人玩.它不能超过随机播放的次数1,追求最大业绩预期 (1 ≤ N ≤ 10,1 ≤ M ≤ 1000). 主题链接:pid=5045" ...
- [ACM] hdu 5045 Contest (减少国家Dp)
Contest Problem Description In the ACM International Collegiate Programming Contest, each team consi ...
- HDU–5988-Coding Contest(最小费用最大流变形)
Coding Contest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- HDU 4309 Seikimatsu Occult Tonneru 网络流量+像缩进
主题链接:点击打开链接 意甲冠军: 题意:给出一张N(N<=100)个点,M(M<=1000条)边的有向图. 每一个点上都有一些人.每条边有4个属性(u,v,w,p). 这些边分为三种:( ...
- hdu 5045 Contest(状态压缩DP)
题解:我们使用一个二位数组dp[i][j]记录进行到第i个任务时,人组合为j时的最大和(这里的j我们用二进制的每位相应一个人). 详细见代码: #include <iostream> #i ...
- HDU 5045 Contest
pid=5045">主题链接~~> 做题感悟:比赛时这题后来才写的,有点小尴尬.两个人商议着写写了非常久才写出来,I want to Powerful ,I believe me ...
随机推荐
- bzoj 1266 1266: [AHOI2006]上学路线route
1266: [AHOI2006]上学路线route Time Limit: 3 Sec Memory Limit: 162 MBSubmit: 2356 Solved: 841[Submit][S ...
- 《大话操作系统——做坚实的project实践派》(2)
<大话操作系统--做坚实的project实践派>(2)
- UI组件之AdapterView及其子类(五)ListView组件和ListActivity
ListView组件是一个显示组件,继承AdapterView基类,前面已经介绍了分别使用ArrayAdapter,SimpleAdapter,扩展BaseAdapter来为LisView提供列表项h ...
- hdu2767 Proving Equivalences,有向图强联通,Kosaraju算法
点击打开链接 有向图强联通,Kosaraju算法 缩点后分别入度和出度为0的点的个数 answer = max(a, b); scc_cnt = 1; answer = 0 #include<c ...
- struts2在action中获取request、session、application,并传递数据
假设仅仅是通过request.session.application传递数据,则不须要获取对应的对象也能够传递数据,代码例如以下: ScopeAction.java: package com.ithe ...
- 2015.05.05,外语,读书笔记-《Word Power Made Easy》 15 “如何谈论事情进展” SESSION 42
HOW TO TALK ABOUT WHAT GOES ON TEASER PREVIEW 一些以-ate结束的动词,通常表示: to exhaust([ig'zɔ:st] n. 排气,排气装置 v. ...
- UVA 11728 - Alternate Task 数学
Little Hasan loves to play number games with his friends. One day they were playing a game whereone ...
- angularjs1-2,作用域、代码压缩
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- gridview in webform
How to: Enable Default Paging in the GridView Web Server Control https://msdn.microsoft.com/en-us/li ...
- hdoj--3062--party(2-sat 可行解)
Party Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...