九度题目1154:Jungle Roads

原创 2016年08月29日 14:55:48

纪念一下最小生成树和我的渣渣水平

九度题目1154:Jungle Roads

#include<stdio.h>
#include<iostream>
#include<vector>
#include<string.h>
#include<queue>
using namespace std;
int n,m;
struct edge
{
    int to;
    int d;
    friend bool operator < (const edge &a,const edge &b)
    {
        if(a.d>b.d)
            return true;
        return false;
    }
};
vector<edge>G[30];
vector<edge>::iterator ite;
priority_queue<edge>q;
bool vis[100];
char c;
char tmp;
int dis;
int prim()
{
    int dis = 0;
    edge e;
    e.to =0;
    e.d = 0;
    q.push(e);
    while(!q.empty())
    {
        e = q.top();
        //cout<<e.to<<endl;
        while(1)
        {
            if(vis[e.to]==0)
                break;
            //cout<<"--"<<endl;
            if(q.empty())
            {
                e.d = 0;
                break;
                //cout<<"breakl"<<endl;
            }
            q.pop();
            e=q.top();
        }
        vis[e.to]=1;
//        cout<<e.to<<endl;
//        cout<<e.d<<endl;
        dis += e.d;
        for(ite = G[e.to].begin();ite!=G[e.to].end();ite++)
        {
            edge ee ;
            ee = *ite;
           // cout<<ee.to<<endl;
           if(!vis[ee.to])
            q.push(ee);
        }
    }
   // cout<<"--"<<endl;
    return dis;
}
int main()
{
    while(~scanf("%d",&n)&&n)
    {
        for(int i = 0 ; i <= n;i++)
        G[i].clear();
        memset(vis,0,sizeof(vis));
        for(int i = 1 ; i < n ;i++)
        {
            getchar();
            scanf("%c",&c);
            scanf("%d",&m);
            for(int j = 0 ; j < m ;j++)
            {
                //
                getchar();
                scanf("%c",&tmp);
                //cout<<tmp<<" ";
                scanf("%d",&dis);
                //cout<<dis<<endl;
                edge e;
                e.to = tmp-'A';
                e.d = dis;
                G[c-'A'].push_back(e);
                e.to = c-'A';
                G[tmp-'A'].push_back(e);
            }
        }
//        cout<<"---"<<endl;
        cout<<prim()<<endl;
    }
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

九度OJ 题目1154:Jungle Roads

 一.题目描述:         The Head Elder of the tropical island of Lagrishan has a problem. A burst o...
  • lhyer
  • lhyer
  • 2015年09月05日 15:40
  • 156

九度教程(73)题目1154:Jungle Roads,最小生成树

题目描述:         The Head Elder of the tropical island of Lagrishan has a problem. A burst of...
  • fnzsjt
  • fnzsjt
  • 2014年09月15日 19:10
  • 419

九度 题目1154:Jungle Roads

题目描述:         The Head Elder of the tropical island of Lagrishan has a problem. A burst of...

九度OJ 1154:Jungle Roads(丛林路径) (最小生成树)

时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:832 解决:555 题目描述:         The Head Elder of...

九度OJ-1154:Jungle Roads

题目描述: The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid m...

九度OJ 1154 Jungle Roads

#include #include using namespace std; int Tree[27]; int findRoot(int x) { if(Tree[x] == -1) ret...

hdu 1301 Jungle Roads 一个很水的题 AC率也可以知道了 但是题目很难理解啊 英语渣o(╯□╰)oKruskal算法

The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was s...

Jungle Roads 最小生成树

The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was s...

Jungle Roads 【最小生成树】(构造图)

Jungle Roads The Head Elder of the tropical island of Lagrishan has a problem. A burs...

kruskal_poj 1251Jungle Roads

poj1251 http://poj.org/problem?id=1251 此题一看就知是最小生成树,由于数据量很小,输入的数据形式也没有那一种比较方便,所以prim和kruskal都可以 ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:九度题目1154:Jungle Roads
举报原因:
原因补充:

(最多只允许输入30个字)