bzoj 3396: [Usaco2009 Jan]Total flow 水流 网络流
2017-09-04 08:24
281 查看
→题目链接←
看到题面后感觉可能有什么奇技淫巧...?
但是懒得想
数据辣么小
直接网络流搞一搞就行了
代码:
#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
#define ll long long
#define inf 10000001
using namespace std;
struct node{
int to,num;
}edge[11000],edge1[11000];
int n,m;
int s,e;
int pn=1;
vector<int>v[11000];
int edgenum=0;
int d[11000];
void link(int x,int y,int z){
node t;
t.to=y;
t.num=z;
edge[edgenum]=t;
v[x].push_back(edgenum);
edgenum++;
t.to=x;
t.num=0;
edge[edgenum]=t;
v[y].push_back(edgenum);
edgenum++;
}
bool bfs(){
memset(d,0,sizeof(d));
d[s]=1;
queue<int>q;
q.push(s);
while(!q.empty()){
int t=q.front();
q.pop();
if(t==e)continue;
for(int i=0; i<v[t].size(); i++){
int x=v[t][i];
int to=edge[x].to;
int num=edge[x].num;
if(num==0 || d[to]!=0)continue;
d[to]=d[t]+1;
q.push(to);
}
}
if(d[e]!=0)return true;
return false;
}
ll dfs(int x,int y){
if(x==e || y==0)return y;
ll sum=0;
for(int i=0; i<v[x].size(); i++){
int j=v[x][i];
int to=edge[j].to;
int num=edge[j].num;
if(num){
if(d[to]!=d[x]+1)continue;
ll k=dfs(to,min(num,y));
y-=k;
sum+=k;
edge[j].num-=k;
edge[j^1].num+=k;
if(y==0)break;
}
}
if(sum==0)d[x]=0;
return sum;
}
long long ans=0;
int main(){
scanf("%d",&n);
for(int i=1; i<=n; i++){
char x,y;
int z;
cin>>x>>y>>z;
int xx,yy;
if(x>='a' && x<='z')xx=(int)x-'a';
else xx=(int)x-'A'+26;
if(y>='a' && y<='z')yy=(int)y-'a';
else yy=(int)y-'A'+26;
link(xx,yy,z);
}
s=26;
e=25+26;
while(bfs())ans+=dfs(s,inf);
printf("%lld\n",ans);
return 0;
}
看到题面后感觉可能有什么奇技淫巧...?
但是懒得想
数据辣么小
直接网络流搞一搞就行了
代码:
#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
#define ll long long
#define inf 10000001
using namespace std;
struct node{
int to,num;
}edge[11000],edge1[11000];
int n,m;
int s,e;
int pn=1;
vector<int>v[11000];
int edgenum=0;
int d[11000];
void link(int x,int y,int z){
node t;
t.to=y;
t.num=z;
edge[edgenum]=t;
v[x].push_back(edgenum);
edgenum++;
t.to=x;
t.num=0;
edge[edgenum]=t;
v[y].push_back(edgenum);
edgenum++;
}
bool bfs(){
memset(d,0,sizeof(d));
d[s]=1;
queue<int>q;
q.push(s);
while(!q.empty()){
int t=q.front();
q.pop();
if(t==e)continue;
for(int i=0; i<v[t].size(); i++){
int x=v[t][i];
int to=edge[x].to;
int num=edge[x].num;
if(num==0 || d[to]!=0)continue;
d[to]=d[t]+1;
q.push(to);
}
}
if(d[e]!=0)return true;
return false;
}
ll dfs(int x,int y){
if(x==e || y==0)return y;
ll sum=0;
for(int i=0; i<v[x].size(); i++){
int j=v[x][i];
int to=edge[j].to;
int num=edge[j].num;
if(num){
if(d[to]!=d[x]+1)continue;
ll k=dfs(to,min(num,y));
y-=k;
sum+=k;
edge[j].num-=k;
edge[j^1].num+=k;
if(y==0)break;
}
}
if(sum==0)d[x]=0;
return sum;
}
long long ans=0;
int main(){
scanf("%d",&n);
for(int i=1; i<=n; i++){
char x,y;
int z;
cin>>x>>y>>z;
int xx,yy;
if(x>='a' && x<='z')xx=(int)x-'a';
else xx=(int)x-'A'+26;
if(y>='a' && y<='z')yy=(int)y-'a';
else yy=(int)y-'A'+26;
link(xx,yy,z);
}
s=26;
e=25+26;
while(bfs())ans+=dfs(s,inf);
printf("%lld\n",ans);
return 0;
}
相关文章推荐
- 【网络流】【Dinic】【最大流】bzoj3396 [Usaco2009 Jan]Total flow 水流
- bzoj3396 [Usaco2009 Jan]Total flow 水流(最大流)
- 【BZOJ】3396: [Usaco2009 Jan]Total flow 水流 (最大流)
- 【BZOJ3396】【Usaco2009 Jan】Total flow 水流
- bzoj 3396: [Usaco2009 Jan]Total flow 水流(最大流)
- BZOJ3396: [Usaco2009 Jan]Total flow 水流
- bzoj3396 [Usaco2009 Jan]Total flow 水流
- [BZOJ3396][Usaco2009 Jan]Total flow 水流(最大流)
- bzoj3396 [Usaco2009 Jan]Total flow 水流
- bzoj 3396 [Usaco2009 Jan]Total flow 水流
- bzoj3396 [Usaco2009 Jan]Total flow 水流
- bzoj 3396: [Usaco2009 Jan]Total flow 水流【最大流】
- bzoj3396 [Usaco2009 Jan]Total flow 水流
- BZOJ3396: [Usaco2009 Jan]Total flow 水流 网络流
- 3396: [Usaco2009 Jan]Total flow 水流
- 3396: [Usaco2009 Jan]Total flow 水流 (最大流)
- BZOJ3396 [Usaco2009 Jan]Total flow 水流
- [BZOJ1575] [Usaco2009 Jan]气象牛Baric(DP)
- bzoj 1574: [Usaco2009 Jan]地震损坏Damage
- BZOJ3393 [Usaco2009 Jan]Laserphones 激光通讯