Constructing Roads (Prim+heap)

版权声明:你会感谢现在努力的自己 https://blog.csdn.net/qq_41222001/article/details/79976001

                                                Constructing Roads   POJ - 2421
There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are connected, if and only if there is a road between A and B, or there exists a village C such that there is a road between A and C, and C and B are connected. 

We know that there are already some roads between some villages and your job is the build some roads such that all the villages are connect and the length of all the roads built is minimum.
Input
The first line is an integer N (3 <= N <= 100), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 1000]) between village i and village j. 

Then there is an integer Q (0 <= Q <= N * (N + 1) / 2). Then come Q lines, each line contains two integers a and b (1 <= a < b <= N), which means the road between village a and village b has been built.
Output
You should output a line contains an integer, which is the length of all the roads to be built such that all the villages are connected, and this value is minimum.
Sample Input
3
0 990 692
990 0 179
692 179 0
1
1 2
Sample Output
179

最小生成树水题 这里使用下prim+heap优化。之前就写过这些模板题,今天用优先队列优化一下。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<string>
#include<queue>
#include<vector>
#include<stack>
#include<list>
#define mem(a,b) memset(a,b,sizeof(a))
#define P pair<int ,int>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=100+10;
int n,m;
int mp[maxn][maxn],vis[maxn],dis[maxn];
int read()
{
    int xx=0,ff=1;
    char ch;
    ch=getchar();
    while(ch>'9'||ch<'0')
    {
        if(ch=='-') ff=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        xx=(xx<<3)+(xx<<1)+ch-'0';
        ch=getchar();
    }
    return xx*ff;
}
void init()
{
    mem(vis,0);
    mem(dis,INF);
    mem(mp,INF);
}
void prim()
{
    priority_queue<P,vector< P > ,greater< P > >q;
    int cost=0;
    for(int i=1; i<=n; i++)
        dis[i]=mp[1][i];
        q.push(P(0,1));
        while(!q.empty())
        {
            P p=q.top();
            q.pop();
            int v=p.second;
            if(vis[v]) continue;
            vis[v]=1;
            cost+=p.first;
            for(int i=1;i<=n;i++)
            {
                if(!vis[i]&&mp[v][i]!=INF)
                {
                    q.push(P(min(mp[v][i],dis[i]),i));
                }
            }
        }
    printf("%d\n",cost);
}
int main()
{
    int m;
    while(~scanf("%d",&n))
    {
        init();
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
                mp[i][j]=read();
        m=read();
        for(int i=0; i<m; i++)
        {
            int u=read();
            int v=read();
            mp[u][v]=mp[v][u]=0;
        }
        prim();
    }
}

阅读更多

Constructing Roads

08-29

DescriptionnnIn the days of yore, Han was a prosperous kingdom. But at the turn of the century, hard times fell upon the kingdom. A plague swept through the land, and barbarians galloped in from the north, burning farms, destroying roads, and pillaging villages. All that remained were a few isolated strongholds scattered throughout the land. It has now been nearly a decade since the last wave of barbarians stormed through, and the land breathes a sigh of relief. People are becoming revitalized with the hope that they can once again transform the kingdom back into its former glory.nnSince all the roads were destroyed, the strongholds were left in isolation, so the first order of business was to build a network of roads connecting all the strongholds. Each stronghold thought it would be a reasonable plan to start by building a road to the closest stronghold near it. If there were two strongholds of equal distance, the stronghold whose name comes before the other in the dictionary would be chosen. Each stronghold builds at its own rate, measured in feet/hour. Because they wished to finish as soon as possible, construction happened 24 hours a day, continuously advancing the construction site (the end of the road) towards the destination. Of course, construction on a road would stop if the road ran into another road or a city. At the beginning of the New Year, there was a big celebration, and all the strongholds began construction at the same time.nnLittle did the people of Han know that the barbarians had again infiltrated their kingdom and were carefully observing the progress of the roads. The barbarians were curious about the progress of the roads. In particular, they wanted answers to two type of questions.nnAfter exactly t hours since construction began on the New Year, what is the absolute minimum length of additional roads that still need to be built in order to connect all cities? These additional roads are allowed to join two cities, two construction sites, or a city and a construction site.nWhat is the fewest number of hours that must elapse since the New Year before the minimum length of additional roads that still need to be built is at most l?nWrite a program to answer these questions given Han’s construction plan.nnInputnnThere will be several test cases, each representing a possible scenario for Han. The first line of each test case will contain a positive integer number N, the number of strongholds (1 ≤ N ≤ 2 000). Each of the subsequent N lines will contain a description of a stronghold: a name consisting of letters ‘ a’ … ‘z’, the x and y coordinates of the position of the stronghold (in feet), and the construction rate (in feet/hour). The next lines will contain questions. The first integer on the line is either 1 or 2, representing the type of question. For type 1 questions, the next number is t, the time (in hours) in question. For type 2 questions, the next number is l, the length (in feet) in question. The questions will be terminated by a line with 0.nnThe input data is terminated by a line that contains one zero, and should not be processed.nnOutputnnFor each test case, output the answers to each question, formatted as in the sample output. If for question 2, at no point in time will there be only l feet of construction left, then print NEVER. All numeric answers should be printed to as many decimal places as you feel necessary. To get credit for the problem, however, your answer must be within 0.01 of our answer.nnSample Inputnn4nportland 0 0 3nseattle 0 10 2nnewyork 20 6 1nboston 20 0 1n1 0n1 2.0n1 3.0n2 29n2 1.0n0n2nbree -10 -10 1nbuckland 10 10 2n1 5n0n0nSample OutputnnKingdom 1n36.000 feet left at time 0.000n22.000 feet left at time 2.000n20.000 feet left at time 3.000n1.000 hours before 29.000 feet leftnNEVERnnKingdom 2n13.284 feet left at time 5.000nnEnd

没有更多推荐了,返回首页