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;
}

 

全部评论

相关推荐

点赞 评论 收藏
分享
shtdbb_:还不错,没有让你做了笔试再挂你
点赞 评论 收藏
分享
评论
点赞
1
分享
正在热议
# 25届秋招总结 #
442870次浏览 4513人参与
# 春招别灰心,我们一人来一句鼓励 #
42047次浏览 534人参与
# 北方华创开奖 #
107451次浏览 600人参与
# 地方国企笔面经互助 #
7969次浏览 18人参与
# 同bg的你秋招战况如何? #
76925次浏览 565人参与
# 实习必须要去大厂吗? #
55786次浏览 961人参与
# 阿里云管培生offer #
120335次浏览 2220人参与
# 虾皮求职进展汇总 #
115973次浏览 886人参与
# 如果你有一天可以担任公司的CEO,你会做哪三件事? #
11632次浏览 289人参与
# 实习,投递多份简历没人回复怎么办 #
2454828次浏览 34858人参与
# 提前批简历挂麻了怎么办 #
149917次浏览 1978人参与
# 在找工作求抱抱 #
906063次浏览 9421人参与
# 如果公司给你放一天假,你会怎么度过? #
4760次浏览 55人参与
# 你投递的公司有几家约面了? #
33209次浏览 188人参与
# 投递实习岗位前的准备 #
1195992次浏览 18550人参与
# 机械人春招想让哪家公司来捞你? #
157640次浏览 2267人参与
# 双非本科求职如何逆袭 #
662310次浏览 7397人参与
# 发工资后,你做的第一件事是什么 #
12786次浏览 62人参与
# 工作中,努力重要还是选择重要? #
35857次浏览 384人参与
# 简历中的项目经历要怎么写? #
86928次浏览 1516人参与
# 参加完秋招的机械人,还参加春招吗? #
20142次浏览 240人参与
# 我的上岸简历长这样 #
452040次浏览 8089人参与
牛客网
牛客企业服务