• Travel Problem[SZU_K28]


    Description
    After SzuHope take part in the 36th ACMICPC Asia Chendu Reginal Contest. Then go to QingChengShan for fun. QCS has many spot as the picture following, each spot has its unique value for SzuHope, they will get the value just once if and only if they had been there. From one spot will exists some roads to other spots, it means that one road link two spots without direction. SzuHope can choose any spot to travel at beginning, but then they can go to next spot only by roads. Can you help them make the travel’s total value biggest?


    Input
    There are less than 100 test cases. For each case, the first line has two numbers M,N.describe the number of spots and roads(1<=N<=1000, 0<=M<=N*N) , the spots are numbered with 1,2,3…N; the second line has N numbers describe the value xi of each spot(0<xi<1000); the next M line, each line has two numbers U,V(1<= U,V<=N) means U,V is connected. 0 0 for end.


    Output
    For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the result.


    Sample Input
    0 1
    5
    1 3
    1 3 2
    1 3
    0 0

    Sample Output
    Case 1:
    5
    Case 2:
    3

     

    #include<stdio.h>
    #include<string.h>
    int s[1025],father[1025],w[1025];
    int getfather(int v)
    {
    	if (father[v]==v) return father[v];
    	father[v]=getfather(father[v]);
    	return father[v];
    }
    void merge(int u,int v)
    {
    	u=getfather(u);
    	v=getfather(v);
    	father[u]=v;
    }
    int main()
    {
    	int N,M,i,cas=0;
    	while (scanf("%d%d",&M,&N)!=EOF && M+N)
    	{
    		cas++;
    		printf("Case %d:
    ",cas);
    		for (i=1;i<=N;i++) scanf("%d",&w[i]);
    		for (i=1;i<=N;i++) father[i]=i;
    		for (i=1;i<=M;i++)
    		{
    			int u,v;
    			scanf("%d%d",&u,&v);
    			merge(u,v);
    		}
    		for (i=1;i<=N;i++) father[i]=getfather(i);
    		memset(s,0,sizeof(s));
    		for (i=1;i<=N;i++) s[father[i]]+=w[i];
    		int Max=0;
    		for (i=1;i<=N;i++)
    			if (s[i]>Max) Max=s[i];
    		printf("%d
    ",Max);
    	}
    	return 0;
    }
    

     

  • 相关阅读:
    mmsplayer V2 for android 抢先发布
    mmsplayer v2 windows C 之(mmsplayer_wave )
    mmsplayer v2 java 之(MmsplayerActivity Activity类)
    mmsplayer V2 for windows 发布
    今天休息,明天晚上继续IOS版本mmsplayer V2
    mmsplayer v2 java 之(MmsplayerActivity Activity类)
    mmsplayer V2 for windows 发布
    自定义函数:删除数组B中与数组A重复的值
    LotusScript方法扩展
    MS SQL 2005 SP2新功能
  • 原文地址:https://www.cnblogs.com/dramstadt/p/3225704.html
Copyright © 2020-2023  润新知