九度题目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
  • 137

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

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

九度 题目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...

九度:1154<最小生成树>

http://ac.jobdu.com/problem.php?pid=1154 // 北大10机试 // 1154:Jungle Roads // // WA:原因,初始化时为1~SIZE-...

sicily 题目之 1154 1200 1324

1154. Easy sort   Total: 6575Accepted:1998Rating:2.7/5.0(67 votes)

poj 1251 Jungle Roads(最小生成树)

The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was s...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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