Jungle Roads


The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extra roads between villages some years ago. But the jungle overtakes roads relentlessly, so the large road network is too expensive to maintain. The Council of Elders must choose to stop maintaining some roads. The map above on the left shows all the roads in use now and the cost in aacms per month to maintain them. Of course there needs to be some way to get between all the villages on maintained roads, even if the route is not as short as before. The Chief Elder would like to tell the Council of Elders what would be the smallest amount they could spend in aacms per month to maintain roads that would connect all the villages. The villages are labeled A through I in the maps above. The map on the right shows the roads that could be maintained most cheaply, for 216 aacms per month. Your task is to write a program that will solve such problems. 

The input consists of one to 100 data sets, followed by a final line containing only 0. Each data set starts with a line containing only a number n, which is the number of villages, 1 < n < 27, and the villages are labeled with the first n letters of the alphabet, capitalized. Each data set is completed with n-1 lines that start with village labels in alphabetical order. There is no line for the last village. Each line for a village starts with the village label followed by a number, k, of roads from this village to villages with labels later in the alphabet. If k is greater than 0, the line continues with data for each of the k roads. The data for each road is the village label for the other end of the road followed by the monthly maintenance cost in aacms for the road. Maintenance costs will be positive integers less than 100. All data fields in the row are separated by single blanks. The road network will always allow travel between all the villages. The network will never have more than 75 roads. No village will have more than 15 roads going to other villages (before or after in the alphabet). In the sample input below, the first data set goes with the map above. 

The output is one integer per line for each data set: the minimum cost in aacms per month to maintain a road system that connect all the villages. Caution: A brute force solution that examines every possible set of roads will not finish within the one minute time limit. 

Input

9
A 2 B 12 I 25
B 3 C 10 H 40 I 8
C 2 D 18 G 55
D 1 E 44
E 2 F 60 G 38
F 0
G 1 H 35
H 1 I 35
3
A 2 B 10 C 40
B 1 C 20
0

Output

216
30

Sample Input

9
A 2 B 12 I 25
B 3 C 10 H 40 I 8
C 2 D 18 G 55
D 1 E 44
E 2 F 60 G 38
F 0
G 1 H 35
H 1 I 35
3
A 2 B 10 C 40
B 1 C 20
0

Sample Output

216
30

C++版本一

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
using namespace std;
const int N=30;
int m,n,pre[N];

struct node{
    int f,t,w;
    node(){};
    node(char a,char b,int c){
        f=a-64;
        t=b-64;
        w=c;
    }
    bool operator < (const node &S )const{

        return w<S.w;
    }
}e[N*N];
int find(int x){
    int r=x;
    while(pre[r]!=r)
        r=pre[r];
    return r;
}
void join(int x,int y){
    int fx=find(x);
    int fy=find(y);
    if(fx!=fy)
        pre[fx]=fy;
}
int main()
{
    while(scanf("%d",&n)!=EOF&&n){
        int k,l;
        char c,d;
        int cnt=0;
        for(int i=1;i<n;i++){
            cin>>c>>k;
            for(int j=1;j<=k;j++){
                cin>>d>>l;
                e[++cnt]=node(c,d,l);
            }
        }
        sort(e+1,e+cnt+1);
        for(int i=1;i<=n;i++)
            pre[i]=i;
        int ans=0;
        for(int i=1;i<=cnt;i++)
            if(find(e[i].f)!=find(e[i].t)){
                join(e[i].f,e[i].t);
                ans+=e[i].w;
            }
        cout << ans << endl;

    }
    //cout << "Hello world!" << endl;
    return 0;
}

C++版本二

Prime算法

#include <cstdio>//prime
#include <iostream>
using namespace std;
const int INF=0x3f3f3f3f;
int dis[30];
int a[30][30];
bool vis[30];
int n,m;
int Prime()
{
    for(int i=0; i<n; i++)
    {
        dis[i]=a[0][i];
        vis[i]=false;
    }
    dis[0]=0;
    vis[0]=true;
    int ans=0;
    for(int i=1; i<n; i++)
    {
        int p=-1;
        int minn=INF;
        for(int j=0; j<n; j++)
        {
            if(!vis[j]&&dis[j]<minn)
                minn=dis[p=j];
        }
        ans+=minn;
        vis[p]=true;
        for(int j=1; j<n; j++)
        {
            if(!vis[j]&&dis[j]>a[p][j])
                dis[j]=a[p][j];
        }
    }
    return ans;
}
int main()
{
    while(cin>>n,n)
    {
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<n; j++)
                if(i==j) a[i][j]=0;
                else a[i][j]=INF;
        }
        char ch1,ch2;
        int x,y;
        for(int i=1; i<n; i++)
        {
            cin>>ch1>>x;
            while(x--)
            {
                cin>>ch2>>y;
                if(a[ch1-'A'][ch2-'A']>y)
                    a[ch1-'A'][ch2-'A']=a[ch2-'A'][ch1-'A']=y;
            }
        }
        printf("%d\n",Prime());
    }
    return 0;
}

C++版本三

#include <iostream>
#include <algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
struct node
{
    int s,e,w;
}a[500];
int fa[30];
int n,m;
int Find(int x)
{
    if(x==fa[x])
        return x;
    return fa[x]=Find(fa[x]);
}
bool cmp(node a,node b)
{
    return a.w<b.w;
}
int Kruskal()
{
    sort(a,a+m,cmp);
    int ans=0;
    for(int i=0;i<m;i++)
    {
        int fx=Find(a[i].s);
        int fy=Find(a[i].e);
        if(fx!=fy)
        {
            ans+=a[i].w;
            fa[fx]=fy;
        }
    }
    return ans;
}
int main()
{
    while(cin>>n,n)
    {
        int x,y;
        char ch1,ch2;
        for(int i=0;i<n;i++)
            fa[i]=i;
        m=0;
        for(int i=1;i<n;i++)
        {
            cin>>ch1>>x;
            while(x--)
            {
                cin>>ch2>>y;
                a[m].s=ch1-'A';
                a[m].e=ch2-'A';
                a[m++].w=y;
            }
        }
        cout<<Kruskal()<<endl;
    }
    return 0;
}

 

全部评论

相关推荐

2025-12-08 16:04
门头沟学院 Java
本人本科末9,今年大三。大一大二一直玩,什么都没学到,在大学混日子混了两年,每天不是在打农就是在steam。大三开学时一个和自己玩的好的同学去实习了,才发现自己白白浪费了两年的时间,如果真不冲一下就真去京东,阿里,美团送外卖了今年9月份开始学Java,一开始一直跟着黑马视频看,后面发现看视频效率太低了,时间根本不够,就开始主要看文档和看书了。这几个月一直在学,真的尽力了,希望暑期前能找一份好点的实习。我简历上面的项目大多没有指标,但是实际上我是真没多少时间去做项目,我基本主要是动手只做了外卖和天机,黑马点评和12306我都是只是看了项目。主要是自己的时间真的不多,但是这样子自己的代码能力确实比较差。而且自己也没有做过实际的工程,我顶多用jmeter测试一下接口tps啥的,比如使用Redis管道提升了一点性能,减少Redis交互,这种值得写上去吗?需不需要具体到某些数字求求各位佬给一些建议,看看简历怎么优化?项目介绍是不是不够详细?没有具体到业务方面。项目会不会提到大致实现原理导致面试官一看简历就知道怎么实现就没有问的欲望?专业技能一些字段是不是要加粗,是不是写太啰嗦了?有没有必要压缩内容变成一页?两页的话是不是都要把两页填地满满的。
给秋招一个交代:一页简历最好,网上做的项目放面试官眼里都是玩具,简历上不需要强调有什么难点,记住就行防止真的问。然后背八股,多投多面试就行
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务