HDU 3371 connect the cities (Minimum Spanning Tree prim)

Source: Internet
Author: User

Question connection: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 3371

 

It is not difficult to note that we should define the cost between cities as 0, and use the common prim algorithm. I didn't use the Kruskal's algorithm ), because the data for this question is relatively large, and a large amount of data needs to be processed to make it 0, for fear of timeout t ^ t .....

 

 

 1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4  5 using namespace std; 6  7 #define inf 999999 8  9 int map[505][505],node[505],n,vis[505];10 int dd[505],sum;11 12 int Prim()13 {14     int tm=1;15     node[tm]=0;16     //vis[tm]=1;17     for(int k=1;k<=n;k++)18     {19         int minn=inf;20         for(int i=1;i<=n;i++)21         if(!vis[i]&&minn>node[i])22         {23             tm=i;24             minn=node[i];25         }26         vis[tm]=1;27         if(minn==inf)28         return 0;29 30         sum+=minn;31 32         for(int i=1;i<=n;i++)33         {34             if(!vis[i]&&node[i]>map[tm][i])35             node[i]=map[tm][i];36         }37     }38     return 1;39 }40 41 int main()42 {43     int T;44     scanf("%d",&T);45     while(T--)46     {47         int m,k;48         scanf("%d%d%d",&n,&m,&k);49         for(int i=1;i<=n;i++)50         {51             node[i]=inf;vis[i]=0;52             for(int j=1;j<=n;j++)53             map[i][j]=inf;54         }55         while(m--)56         {57             int a,b,d;58             scanf("%d%d%d",&a,&b,&d);59             if(map[a][b]>d)60             map[a][b]=map[b][a]=d;61         }62         while(k--)63         {64             int t;65             scanf("%d",&t);66             for(int i=1;i<=t;i++)67             {68                 scanf("%d",&dd[i]);69             }70             for(int i=1;i<=t;i++)71             {72                 for(int j=i+1;j<=t;j++)73                 map[dd[i]][dd[j]]=map[dd[j]][dd[i]]=0;74             }75         }76         sum=0;77         int flag=Prim();78         if(flag==0)79         printf("-1\n");80         else81         printf("%d\n",sum);82     }83     return 0;84 }

 

HDU 3371 connect the cities (Minimum Spanning Tree prim)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.