全球彩票平台_全球彩票注册平台|官网下载地址

热门关键词: 全球彩票平台,全球彩票注册平台,全球彩官网下载地址

全球彩票注册平台最小生成树

【ZOJ 2966】Build The Electric System(最小生成树 Kruskal ),zojkruskal

Disaster Area Reconstruction
There was a large earthquake in Sichuan last May. All the things were ruined in that disaster, the buildings, the railways and the establishments.

The Battle of Guandu
Problem Description
In the year of 200, two generals whose names are Cao Cao and Shao Yuan are fighting in Guandu. The battle of Guandu was a great battle and the two armies were fighting at M different battlefields whose numbers were 1 to M. There were also N villages nearby numbered from 1 to N. Cao Cao could train some warriors from those villages to strengthen his military. For village i, Cao Cao could only call for some number of warriors join the battlefield xi. However, Shao Yuan's power was extremely strong at that time. So in order to protect themselves, village i would also send equal number of warriors to battlefield yi and join the Yuan Shao's Army. If Cao Cao had called for one warrior from village i, he would have to pay ci units of money for the village. There was no need for Cao Cao to pay for the warriors who would join Shao Yuan's army. At the beginning, there were no warriors of both sides in every battlefield.

Power Cable Problem
Description

Intermediate Rounds for Multicasting
Problem Description
Consider a communication network consisting of N nodes numbered from 1 to N. The nodes are interconnected in such a way that the network has the shape of a rooted tree, with node 1 as the root. Node 1 wants to send a message (the same message) to each node which is a leaf in the tree (i.e. has no sons) – this operation is known as multicast. A message can only be sent from one node to one of its descendants (including the node itself). Each edge of the tree has an associated cost and the cost of sending a message from a node X to one of its descendants Y is the sum of the costs of the edges on the unique path from X to Y (if X=Y, then the cost is 0). The total cost of a multicast strategy is the sum of the costs of sending each message.
In order to reach its goal, node 1 will use the following multicast strategy: The strategy consists of K intermediate rounds. In the first round, node 1 sends an individual message to a subset of nodes S1 such that each leaf is a descendant of exactly one node X in S1 (this means that any node X in S1 is not a descendant of another node Y in S1). In round i (2<=i<=K), each node X in Si-1 sends an individual message to a subset Si,X of nodes from its subtree, such that each leaf which is a descendant of X is also a descendant of exactly one node in Si,X. The set of nodes Si is the union of the sets Si,X, for each X in Si-1. In the end, each node X in Sk must send a message to each leaf node which is a descendant of X.
Given the communication network, the cost of each edge and the number of intermediate rounds K, find the minimum total cost of a multicast strategy.

Description

In last winter, there was a big snow storm in South China. The electric system was damaged seriously. Lots of power lines were broken and lots of villages lost contact with the main power grid. The government wants to reconstruct the electric system as soon as possible. So, as a professional programmer, you are asked to write a program to calculate the minimum cost to reconstruct the power lines to make sure there's at least one way between every two villages.

Input

Standard input will contain multiple test cases. The first line of the input is a single integer T (1 <= T <= 50) which is the number of test cases. And it will be followed by T consecutive test cases.

In each test case, the first line contains two positive integers N and E (2 <= N <= 500, N <= E <= N * (N - 1) / 2), representing the number of the villages and the number of the original power lines between villages. There follow E lines, and each of them contains three integers, ABK (0 <= AB < N, 0 <= K < 1000). A and B respectively means the index of the starting village and ending village of the power line. If K is 0, it means this line still works fine after the snow storm. If K is a positive integer, it means this line will cost Kto reconstruct. There will be at most one line between any two villages, and there will not be any line from one village to itself.

Output

For each test case in the input, there's only one line that contains the minimum cost to recover the electric system to make sure that there's at least one way between every two villages.

Sample Input

1
3 3
0 1 5
0 2 0
1 2 9

In order to send the rescue materials to where the disaster was, the traffic had to be reconstructed first. But just after the earthquake, the government spent a lot of money propitiating the refugees and could not afford much on reconstruction. After investigating the remaining roads, the government decided to make one new road so that the maximum connected component would be maximized by this new road (in a connected component, each village has directed paths to any other villages). Now your task is to determine how the new road is to be constructed.

As one of greatest strategist at that time, Cao Cao was considering how to beat Shao Yuan. As we can image, the battlefields would have different level of importance wi. Some of the battlefields with wi=2 were very important, so Cao Cao had to guarantee that in these battlefields, the number of his warriors was greater than Shao Yuan's. And some of the battlefields with wi=1 were not as important as before, so Cao Cao had to make sure that the number of his warriors was greater or equal to Shao Yuan's. The other battlefields with wi=0 had no importance, so there were no restriction about the number of warriors in those battlefields. Now, given such conditions, could you help Cao Cao find the least number of money he had to pay to win the battlefield?

The downtown of city T consists of N, 1 <= N <= 10000, tall commercial buildings that have basements. The buildings are numbered from 0 through N-1. The electricity of each building is provided by the City Electrical Power Company that puts all of its M, 1 <= M <= 50, power cables underground. In order for a building to have electricity, a power line must be connected from one of the underground cables to a power converter inside the building. Because of technical reasons, each power cable is a loop, meaning that it is a long cable line that originates from a mini power station, runs through some regions in the city and then comes back to the same power station. It is known that each power cable connects to at least 2 and at most 500 buildings. A building may be connected to zero, one or more than one power cable. The electricity of a building connected to more than one power cable can be provided by any one power cable by properly setting its power converter. To have a better city view, it is required by the law that power converters can only be built inside the basements.

Input
The first line of input contains an integer number T, representing the number of test cases to follow. The first line of each test case contains 2 integer numbers: N (1<=N<=333) and K (1<=K<=10). The next N-1 lines contain 3 integers each: A, B and C (1<=C<=10.000), meaning that node B is a son of node A and the edge has cost C.

Sample Output

5

题意

付出贰个无向边带权图,输出最小生成树

#include<iostream>
#include<algorithm>
using namespace std;
int p[10005],n,e;
struct edge
{
    int x,y,w;
}a[10005];
int cmp(edge a,edge b)
{
    return a.w<b.w;
}
int find(int r)
{
    if(p[r]!=r)
    p[r]=find(p[r]);
    return p[r];
}
int k()
{
    sort(a,a e,cmp);              //一定要以边edge排序啊!!!
    int ans=0,i;
    for(i=0;i<e;i  )
    {
        int fx=find(a[i].x),fy=find(a[i].y);
        if(fx!=fy)
        {
            p[fx]=fy;
            ans =a[i].w;
        } 
    }
    return ans;
}
int main()  
{  
    int t,i;
    cin>>t;
    while(t--)
    {
        cin>>n>>e;
        for(i=0;i<=n;i  )
            p[i]=i;
        for(i=0;i<e;i  )
            scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].w);
        printf("%dn",k());
    } 
    return 0;
}

2966】Build The Electric System(最小生成树 Kruskal ),zojkruskal Description In last winter, there was a big snow storm in South China. The electric system was da...

NOTICE that:
Each road is one-way, i.e. you can only go from one end of the road to the other end, but can not go in the opposite direction.
No road is built from and to the same village, since it would be useless. But there could be more than one road from one village to another since this can enlarge the traffic capability between these two villages.
Input

Input
The first line of the input gives the number of test cases, T. T test cases follow.

During a Typhoon, the local rain storm, the downtown of city T is flooded. The basements of K, 1 <= K <= N, buildings are filled with water. Fortunately, none of the mini power stations are damaged. Once a basement is flooded with rain water, its power converter is damaged and the building is out of electricity. Before fixing the power converter, we need to drain the water in the basement, which takes at least a long time. To make the situation worse, the power cables of city T are designed with the constraint that for each power cable, if it is connected with a damaged power converter, then none of the power converters connected to this power cable can be turned on. It is also impossible to disconnect the damaged power converts from the power cables. However, it is possible to properly set a power convert to get electricity from a power cable that carries electricity. After Typhoon, the City Electrical Power Company needs to know the total number of buildings that are out of electricity. Since the flood has made the traffics inside the city bad, the company cannot send people to survey. Fortunately, it is known by the company the buildings that are flooded in Typhoon since people from those buildings telephoned the company for help. Giving the original power line connection oor plans and the buildings that are flooded, your task is to calculate the total number of buildings that are currently out of electricity, including the ones that are originally not connecting to any power cable.
For example, each circle in Figure 1 represents a building. Two concentric circles represents a flooded building. There are 9 buildings. Buildings 7 and 8 are flooded. Solid straight lines are power cables. There are 3 power cable lines. One connects buildings 0, 1 and 6. One connects buildings 1, 2, 3 and 7. The last one connects buildings 0, 1, 4, 5 and 8. Buildings 2, 3, 4, 5, 7 and 8 do not have electricity currently in this example.

Output
For each of the T test cases, in the order given in the input, print one line containing the minimum total cost of a multicast strategy having the specified properties.

本文由全球彩票平台发布于全球彩票注册平台编程,转载请注明出处:全球彩票注册平台最小生成树

TAG标签: 全球彩票平台
Ctrl+D 将本页面保存为书签,全面了解最新资讯,方便快捷。