Blue Bridge Cup previous questions City construction (minimum spanning tree)

Source: Internet
Author: User

Previous Questions City construction time limit: 1.0s memory limit: 256.0MBProblem description The building lives in a bustling city of C, however, most of the roads in this town are in disrepair. The mayor was going to rebuild some roads to make it easier for the public, so he found the building and hoped the building would help him.

There are n more important locations in C City, and the mayor wants these sites to be considered. Some roads can now be repaired to connect some of them, with each road connecting two of them. In addition, the city of C has a river through which it is possible to build piers at some of these locations, all of which can be connected by river channels.

The building has access to information about the roads that are allowed to be built, including the cost of each road that can be built, and the costs of building docks and building docks.

The mayor hoped that the building would give a plan that would allow any two locations to reach each other only through new roads or rivers, while at the same time spending as little as possible. Input format the first line of input contains two integers n, m, respectively, the number of important locations in C city and the number of road lines that can be constructed. All locations are numbered from 1 to n sequentially.
The next M-line, three integers a, B, c for each line, indicates that a road from location A to location B can be built, costing C. If c is positive, it means that the construction is spending money, if C is negative, then you can make money after building the road (such as building toll roads).
The next line, containing n integers w_1, w_2, ..., w_n. If the w_i is positive, it means the cost of building the terminal at location I, if the w_i is-1, the location I cannot build the pier.
The input guarantees that there is at least one way to allow any two locations to reach each other only through newly repaired roads or rivers. The output format outputs a line that contains an integer that represents the minimum cost of making all locations through a new road or dock connection. If you can make money if the conditions are met, you should output a negative number. Sample Input 5 5
1 2 4
1 3-1
2 3 3
2 4 5
4 5 10
-1 10 10 1 1 Sample Output 9 example shows the construction of the 2nd, 3, 4 roads, at the site 4, 5 construction of the pier, the total cost is 9. Data size and conventions for 20% of data,1<=n<=10,1<=m<=20,0<=c<=20,w_i<=20;
For 50% of data,1<=n<=100,1<=m<=1000,-50<=c<=50,w_i<=50;
For 70% of data,1<=n<=1000;
For 100% of data, 1 <= n <= 10000,1 <= m <= 100000,-1000<=c<=1000,-1<=w_i<=1000,w_i≠0. AC Code
#include <stdio.h> #include <string.h> #include <stdlib.h> #define INF 0xfffffffint n,m,k;int pre[ 10005];struct s{int u,v,w;} Edge[1000005];int cmp (const void *a,const void *b) {return (* (struct S *) a). W (* (struct S *) b). int find (int x) {if (x==pre[x]) return X;return pre[x]=find (pre[x]);} void init (int n) {int i;for (i=0;i<=n;i++) {pre[i]=i;}} int ku (int k) {int sum=0,i;for (i=0;i<k;i++) {int u=edge[i].u;int v=edge[i].v;int w=edge[i].w;int fa=find (u); int fb= Find (v); if (fa!=fb| | w<0) {SUM+=W;PRE[FA]=FB;}} return sum;} int main () {while (scanf ("%d%d", &n,&m)!=eof) {int i,j;init (n); for (i=0;i<m;i++) {int u,v,w;scanf ("%d%d%d", &AMP;U,&AMP;V,&AMP;W); edge[i].u=u;edge[i].v=v;edge[i].w=w;} K=m;for (i=1;i<=n;i++) {int cost;scanf ("%d", &cost), if (cost!=-1) {edge[k].u=0;edge[k].v=i;edge[k++].w=cost;}} for (i=0;i<m;i++) {int Fa=find (EDGE[I].U), int fb=find (EDGE[I].V), if (FA!=FB) PRE[FA]=FB;} for (i=2;i<=n;i++) {if (Find (1)!=find (i)) {break;}} int Temp;if (i==n+1) {init (n); Qsort (Edge,m,sizeof (EDGE[0]), CMP), Temp=ku (M), Init (n), Qsort (Edge,k,sizeof (edge[0]), CMP), int ans=ku (k), if (ans>temp) printf ("%d\n", temp); elseprintf ("%d\n", ans); continue;} Init (n); Qsort (Edge,k,sizeof (edge[0]), CMP), int ans=ku (k);p rintf ("%d\n", ans);}}


Blue Bridge Cup previous questions City construction (minimum spanning tree)

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.