| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
Re:网上找的一个模板 用的是Dinic算法 可是在HDU可以过 0MS 280 K 但是在PKU却TLE !!我把代码贴出来,向各位大牛求救啊……In Reply To:网上找的一个模板 用的是Dinic算法 可是在HDU可以过 0MS 280 K 但是在PKU却TLE !!我把代码贴出来,向各位大牛求救啊…… Posted by:liuxinbiao at 2009-08-25 18:46:46 #include "iostream"
#include "queue"
using namespace std;
int m,n,a,b,c,cost[201][201];
int Pre[201],Min[201],M=10000001;
bool visit[201];
int Bfs()
{
queue<int> q;
int temp;
visit[1]=true;
Pre[1]=0;
Min[1]=M;
q.push(1);
while(!q.empty())
{
temp=q.front();
q.pop();
for(int i=1;i<=m;i++)
{
if(!visit[i]&&cost[temp][i])
{
Min[i]=(Min[temp]<cost[temp][i]) ? Min[temp] : cost[temp][i];
Pre[i]=temp;
visit[i]=true;
q.push(i);
if(i==m) return Min[m];
}
}
}
if(visit[m] == false) //有这句就AC了,没这句就WA了 WHY?
return -1;
/* 把if(visit[m] == false)
return -1;
改为:
return -1;
就错了,为什么,我觉的IF 语句对这里没影响啊!
哪位大牛解释下。。
*/
}
void Ford_Fulkerson()
{
int f,Max_Flow=0;
while((f=Bfs())!=-1)
{
int temp=m;
Max_Flow+=f;
memset(visit,false,sizeof(visit));
while(temp!=1)
{
int r=Pre[temp];
cost[r][temp]-=f;
cost[temp][r]+=f;
temp=r;
}
}
printf("%d\n",Max_Flow);
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(cost,0,sizeof(cost));
for(int i=1;i<=n;i++)
{
scanf("%d%d%d",&a,&b,&c);
cost[a][b]+=c;
}
Ford_Fulkerson();
}
return 0;
}
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator