Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

wa的请注意!

Posted by yc5_yc at 2013-01-13 13:42:38 on Problem 2914
合并的代码很容易写错!
#include <cstdio>
#include <string.h>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int NMax=550;
int N,M,G[NMax][NMax],dist[NMax],map[NMax];
bool inA[NMax];
int Dinic() {
//1.min=MAXINT,固定一个顶点P 
//2.从点P用类似prim的s算法扩展出“最大生成树”,记录最后扩展的顶点和最后扩展的边 
//3.计算最后扩展到的顶点的切割值(即与此顶点相连的所有边权和),若比min小更新min 
//4.合并最后扩展的那条边的两个端点为一个顶点(当然他们的边也要合并,这个好理解吧?) 
//5.转到2,合并N-1次后结束 
//6.min即为所求,输出min 
	int ret=(~0u>>1),s=1,pre;
	for(int i=1;i<=N;i++) map[i]=i;
	while(N>1) {
		memset(inA,0,sizeof(inA));
	    inA[map[s]]=1;
		int lastx,lasty;pre=s;
		for(int i=1;i<=N;i++) dist[map[i]]=G[map[s]][map[i]];
		for(int I=1;I<N;I++) {
			int next=-1;
			for(int i=2;i<=N;i++) if(!inA[map[i]]&&(next==-1||dist[map[i]]>dist[map[next]])) next=i;
			inA[map[next]]=1;
			for(int i=1;i<=N;i++) if(!inA[map[i]]) dist[map[i]]+=G[map[next]][map[i]];
			if(I==N-1) {lastx=pre;lasty=next;}
			pre=next;
		}
		ret=min(ret,dist[map[lasty]]);
		for(int i=1;i<=N;i++) G[map[lastx]][map[i]]=G[map[i]][map[lastx]]=G[map[lastx]][map[i]]+G[map[i]][map[lasty]];
//就是上一行,千万不要写成G[map[lastx]][map[lasty]]+G[map[i]][map[lasty]]
		map[lasty]=map[N--];
	}
	return ret==(~0u>>1)?0:ret;
}
int main()
{
	while(scanf("%d%d",&N,&M)!=EOF) {
		int x,y,z;
	    memset(G,0,sizeof(G));
		for(int i=1;i<=M;i++) {
			scanf("%d%d%d",&x,&y,&z);
			x++;y++;
			G[x][y]+=z;G[y][x]+=z;
		}
		printf("%d\n",Dinic());
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator