zoukankan      html  css  js  c++  java
  • 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;
    }
    

     

  • 相关阅读:
    [ css 计数器 counter ] css中counter计数器实例演示三
    [ css 计数器 counter ] css中counter计数器实例演示二
    Leetcode 15. 三数之和
    Leetcode 13. 罗马数字转整数
    Leetcode 19. 删除链表的倒数第 N 个结点
    Leetcode 12. 整数转罗马数字
    Leetcode 11. 盛最多水的容器 双指针
    剑指 Offer 68
    剑指 Offer 68
    面试题 04.02. 最小高度树
  • 原文地址:https://www.cnblogs.com/dramstadt/p/3225704.html
Copyright © 2011-2022 走看看