标签:des style http color os io for ar
4 4 2 1 1 0 1 2 0 0 1 3 0 0 2 4 1 -1 3 4 3 -1 4 4 2 1 1 0 1 2 0 0 1 3 3 1 2 4 1 -1 3 4 3 -1
4 0 4 3
解题思路:有n个城市,m个地道,接下来一行告诉你各个城市的初始人数,接下来m行介绍管道。
-1表示管道既可以经过又可以躲藏人。
0表示管道只能经过城市
1表示只能经过1次,再次经过需要花费建立,建立后就可以永久经过了。
解题代码:
根据样例二建立了如图所示的网络图,只需要枚举那个1号型号取与不取的01状态即可,枚举后求最大流。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
const int INF=(1<<30);
const int maxn=110,maxm=1100;
struct edge{
int u,v,f,next;
edge(int u0=0,int v0=0,int f0=0){
u=u0;v=v0;f=f0;
}
}e[4*maxm];
int src,sink,cnt,head[maxn];
void adde(int u,int v,int f){
e[cnt].u=u,e[cnt].v=v,e[cnt].f=f,e[cnt].next=head[u],head[u]=cnt++;
e[cnt].u=v,e[cnt].v=u,e[cnt].f=0,e[cnt].next=head[v],head[v]=cnt++;
}
void init(){
cnt=0;
memset(head,-1,sizeof(head));
}
queue <int> q;
bool visited[maxn];
int dist[maxn];
void bfs(){
memset(dist,0,sizeof(dist));
while(!q.empty()) q.pop();
visited[src]=true;
q.push(src);
while(!q.empty()){
int s=q.front();
q.pop();
for(int i=head[s];i!=-1;i=e[i].next){
int d=e[i].v;
if(e[i].f>0 && !visited[d]){
q.push(d);
dist[d]=dist[s]+1;
visited[d]=true;
}
}
}
}
int dfs(int u,int delta){
if(u==sink) return delta;
else{
int ret=0;
for(int i=head[u];delta && i!=-1;i=e[i].next){
if(e[i].f>0 && dist[e[i].v]==dist[u]+1){
int d=dfs(e[i].v,min(e[i].f,delta));
e[i].f-=d;
e[i^1].f+=d;
delta-=d;
ret+=d;
}
}
return ret;
}
}
int maxflow(){
int ret=0;
while(true){
memset(visited,false,sizeof(visited));
bfs();
if(!visited[sink]) return ret;
ret+=dfs(src,INF);
}
return ret;
}
int n,m;
vector <edge> qiao;
void initial(){
qiao.clear();
init();
src=0;
sink=n+1;
}
void input(){
int u,v,w,id;
for(int i=1;i<=n;i++){
scanf("%d",&w);
adde(src,i,w);
}
for(int i=1;i<=m;i++){
scanf("%d%d%d%d",&u,&v,&w,&id);
if(id==-1){
adde(u,v,INF);
adde(u,sink,w);
}else if(id==0){
adde(u,v,INF);
}else{
qiao.push_back(edge(u,v,w));
}
}
}
void solve(){
int preflow=maxflow();
pair <int,int> p=make_pair(preflow,0);
int newhead[maxn];
vector <edge> tmp;
for(int i=0;i<cnt;i++) tmp.push_back(e[i]);
for(int i=0;i<=n+1;i++) newhead[i]=head[i];
for(int i=0;i<(1<<qiao.size());i++){
int cost=0;
for(int t=0;t<qiao.size();t++){
if(i&(1<<t)){
adde(qiao[t].u,qiao[t].v,INF);
cost+=qiao[t].f;
}else{
adde(qiao[t].u,qiao[t].v,1);
}
}
int tmpflow=maxflow()+preflow;
if(tmpflow>p.first){
p.first=tmpflow;
p.second=cost;
}
else if(tmpflow==p.first) p.second=min(p.second,cost);
cnt=tmp.size();
for(int t=0;t<cnt;t++) e[t]=tmp[t];
for(int t=0;t<=n+1;t++) head[t]=newhead[t];
}
if(p.first!=0) printf("%d %d\n",p.first,p.second);
else printf("Poor Heaven Empire\n");
}
int main(){
while(scanf("%d%d",&n,&m)!=EOF){
initial();
input();
solve();
}
return 0;
}
HDU 4309 Seikimatsu Occult Tonneru(网络流-最大流),布布扣,bubuko.com
HDU 4309 Seikimatsu Occult Tonneru(网络流-最大流)
标签:des style http color os io for ar
原文地址:http://blog.csdn.net/a1061747415/article/details/38387735