解题报告
这题建模实在是好建。,,好贱。。,
给前向星给跪了,纯dinic的前向星居然TLE,sad。,,回头看看优化,。。
矩阵跑过了。2A,sad,,,
/*************************************************************************
> File Name: PowerN.cpp
> Author: _nplus
> Mail: jun18753370216@gmail.com
> Time: 2014年07月19日 星期六 09时30分23秒
************************************************************************/
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
#include<iostream>
#include<algorithm>
#define inf 99999999
#define N 510
#define M N*N
using namespace std;
int edge[N][N],l[N],n,m,nc,np;
int bfs()
{
queue<int >Q;
memset(l,-1,sizeof(l));
while(!Q.empty())
Q.pop();
l[n]=0;
Q.push(n);
while(!Q.empty())
{
int u=Q.front();
Q.pop();
for(int i=0; i<=n+1; i++)
{
if(edge[u][i]&&l[i]==-1)
{
l[i]=l[u]+1;
Q.push(i);
}
}
}
if(l[n+1]>0)return 1;
else return 0;
}
int dfs(int x,int f)
{
if(x==n+1)return f;
int a;
for(int i=0; i<=n+1; i++)
{
if(edge[x][i]&&(l[i]==l[x]+1)&&(a=dfs(i,min(edge[x][i],f))))
{
edge[x][i]-=a;
edge[i][x]+=a;
return a;
}
}l[x]=-1;//加上时间优化了15倍,,。sad,。,
return 0;
}
int main()
{
int i,j,u,v,w;
while(~scanf("%d%d%d%d",&n,&np,&nc,&m))
{
memset(edge,0,sizeof(edge));
for(i=0; i<m; i++)
{
while(getchar()!='(');
scanf("%d,%d)%d",&u,&v,&w);
edge[u][v]=w;
}
for(i=0; i<np; i++)
{
while(getchar()!='(');
scanf("%d)%d",&v,&w);
edge[n][v]=w;
}
for(i=0; i<nc; i++)
{
while(getchar()!='(');
scanf("%d)%d",&u,&w);
edge[u][n+1]=w;
}
int a,flow=0;
while(bfs())
{
while(a=dfs(n,inf))
{
flow+=a;
}
}
printf("%d\n",flow);
}
}
/*************************************************************************
> File Name: PowerN.cpp
> Author: _nplus
> Mail: jun18753370216@gmail.com
> Time: 2014年07月19日 星期六 09时30分23秒
************************************************************************/
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
#include<iostream>
#include<algorithm>
#define inf 99999999
#define N 510
#define M N*N
using namespace std;
int edge[N][N],pre[N],a[N],n,m,nc,np,flow;
void ek()
{
while(1)
{
queue<int >Q;
Q.push(n);
memset(pre,-1,sizeof(pre));
memset(a,0,sizeof(a));
a[n]=inf;
pre[n]=n;
while(!Q.empty())
{
int u=Q.front();
Q.pop();
for(int v=0;v<=n+1;v++)
{
if(edge[u][v]&&!a[v])
{
pre[v]=u;
a[v]=min(a[u],edge[u][v]);
Q.push(v);
}
}
if(a[n+1])break;
}
if(!a[n+1])break;
for(int u=n+1;u!=n;u=pre[u])
{
edge[pre[u]][u]-=a[n+1];
edge[u][pre[u]]+=a[n+1];
}
flow+=a[n+1];
}
}
int main()
{
int i,j,u,v,w;
while(~scanf("%d%d%d%d",&n,&np,&nc,&m))
{
memset(edge,0,sizeof(edge));
for(i=0; i<m; i++)
{
while(getchar()!='(');
scanf("%d,%d)%d",&u,&v,&w);
edge[u][v]=w;
}
for(i=0; i<np; i++)
{
while(getchar()!='(');
scanf("%d)%d",&v,&w);
edge[n][v]=w;
}
for(i=0; i<nc; i++)
{
while(getchar()!='(');
scanf("%d)%d",&u,&w);
edge[u][n+1]=w;
}
int a;
flow=0;
ek();
printf("%d\n",flow);
}
}
| Time Limit: 2000MS | Memory Limit: 32768K | |
| Total Submissions: 22571 | Accepted: 11819 |
Description

Input
Output
Sample Input
2 1 1 2 (0,1)20 (1,0)10 (0)15 (1)20
7 2 3 13 (0,0)1 (0,1)2 (0,2)5 (1,0)1 (1,2)8 (2,3)1 (2,4)7
(3,5)2 (3,6)5 (4,2)7 (4,3)5 (4,5)1 (6,0)5
(0)5 (1)2 (3)2 (4)1 (5)4
Sample Output
15 6
Hint
Source
POJ训练计划1459_Power Network(网络流最大流/Dinic)
原文:http://www.cnblogs.com/lcchuguo/p/5193743.html