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

 

全部评论

相关推荐

11-09 14:54
已编辑
华南农业大学 产品经理
大拿老师:这个简历,连手机号码和照片都没打码,那为什么关键要素求职职位就不写呢? 从上往下看,都没看出自己到底是产品经理的简历,还是电子硬件的简历? 这是一个大问题,当然,更大的问题是实习经历的描述是不对的 不要只是去写实习流程,陈平,怎么去开会?怎么去讨论? 面试问的是你的产品功能点,是怎么设计的?也就是要写项目的亮点,有什么功能?这个功能有什么难处?怎么去解决的? 实习流程大家都一样,没什么优势,也没有提问点,没有提问,你就不得分 另外,你要明确你投的是什么职位,如果投的是产品职位,你的项目经历写的全都是跟产品无关的,那你的简历就没用 你的面试官必然是一个资深的产品经理,他不会去问那些计算机类的编程项目 所以这种四不像的简历,在校招是大忌
点赞 评论 收藏
分享
评论
点赞
1
分享
正在热议
# 25届秋招总结 #
440737次浏览 4493人参与
# 春招别灰心,我们一人来一句鼓励 #
41537次浏览 524人参与
# 阿里云管培生offer #
119909次浏览 2219人参与
# 地方国企笔面经互助 #
7930次浏览 18人参与
# 同bg的你秋招战况如何? #
75684次浏览 552人参与
# 虾皮求职进展汇总 #
114355次浏览 884人参与
# 北方华创开奖 #
107320次浏览 599人参与
# 实习,投递多份简历没人回复怎么办 #
2454094次浏览 34848人参与
# 实习必须要去大厂吗? #
55687次浏览 960人参与
# 提前批简历挂麻了怎么办 #
149836次浏览 1977人参与
# 投递实习岗位前的准备 #
1195754次浏览 18547人参与
# 你投递的公司有几家约面了? #
33181次浏览 188人参与
# 双非本科求职如何逆袭 #
661934次浏览 7394人参与
# 如果公司给你放一天假,你会怎么度过? #
4734次浏览 55人参与
# 机械人春招想让哪家公司来捞你? #
157604次浏览 2267人参与
# 如果你有一天可以担任公司的CEO,你会做哪三件事? #
11381次浏览 271人参与
# 发工资后,你做的第一件事是什么 #
12431次浏览 61人参与
# 工作中,努力重要还是选择重要? #
35621次浏览 384人参与
# 参加完秋招的机械人,还参加春招吗? #
20091次浏览 240人参与
# 我的上岸简历长这样 #
451933次浏览 8088人参与
# 实习想申请秋招offer,能不能argue薪资 #
39241次浏览 314人参与
# 非技术岗是怎么找实习的 #
155852次浏览 2120人参与
牛客网
牛客企业服务