1. 程式人生 > >Poj 1251 Jungle Roads (鄰接表 + 優先佇列 + Prime 最小生成樹)

Poj 1251 Jungle Roads (鄰接表 + 優先佇列 + Prime 最小生成樹)

突然翻出了幾份之前寫的程式碼,拿出來回顧一下。

題意:求保持森林中每個村子都有道路相通,且維修所需要的最少money

#include <iostream>
#include <queue>
using namespace std;

const int INF = 0x5fffffff;  //權值上限  
const int MAXPT = 30;       //頂點數上限  
const int MAXEG = 1000;     //邊數上限 

class Prim     /*鄰接表 + 優先佇列 + Prime 最小生成樹*/
{  
private:  
    int n,e;  //n個點
    int dis[MAXPT],head[MAXPT];
    bool visit[MAXPT];
  
    struct Node  
    {
        int v,dis;
        Node () {}
        Node (int _v,int _dis)
        {
            v=_v;
            dis=_dis;
        }
        bool operator < (const Node a) const
        {
            return dis>a.dis;
        }
    };

    struct Edge
    {
        int v, w, next;
        Edge () {}
        Edge (int _v, int _next, int _w)
        {
            v=_v;
            next=_next;
            w=_w;
        }
    }edges[MAXEG];

public:
	inline void init (int vx)
    {
        n = vx;
        e = 0;
        memset(head,-1,sizeof(int) * (vx + 1));
		memset(visit,false,sizeof(visit));
    }
    
    inline void Add (int u, int v, int w)
    {
        edges[e] = Edge(v, head[u], w);
        head[u] = e++;
    }

	int prim (int src)
	{
		int cost=0,cnt=0;
		Node first;
		priority_queue<Node> Q;
		Q.push (Node(src, 0));
		while (!Q.empty() && cnt < n)
		{
			first=Q.top();
			Q.pop ();
			if (visit[first.v])
				continue;
			cost+=first.dis;
			cnt++;
			visit[first.v]=true;
			
			for (int i=head[first.v];i!=-1;i=edges[i].next)
				if (!visit[edges[i].v])
					Q.push (Node (edges[i].v,edges[i].w));
		}
		return cost;
	}
}ob;


int main ()
{
	int n;
	char str[5];
	while (~scanf("%d",&n),n)
	{
		ob.init (n);
		for (int i=1;i<n;i++)
		{
			int q,x,y,temp;
			scanf("%s%d",str,&q);
			x=str[0]-64;                               //將A變成1,將B變成2,以此類推
			while (q--)
			{
				scanf("%s%d",str,&temp);
				y=str[0]-64;
				ob.Add(x,y,temp);
				ob.Add(y,x,temp);
			}
		}
		printf("%d\n",ob.prim(1));
	}
	return 0;
}


相關推薦

Poj 1251 Jungle Roads (鄰接 + 優先佇列 + Prime 小生成樹)

突然翻出了幾份之前寫的程式碼,拿出來回顧一下。 題意:求保持森林中每個村子都有道路相通,且維修所需要的最少money #include <iostream> #include <queue> using namespace std; const i

【圖論-MST】POJ 1251 Jungle Roads 叢林中的道路,小生成樹,Kruskal演算法

Jungle RoadsTime Limit: 2 Seconds      Memory Limit: 65536 KBThe Head Elder of the tropical island of Lagrishan has a problem. A burst of

POJ 1251 Jungle Roads (小生成樹prim)

enter capital label 模板 fad posit inpu was right The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign ai

POJ-1251 Jungle Roads---MST裸題(需要編號)

i++ span col 其他 init edge target tps lan 題目鏈接: https://vjudge.net/problem/POJ-1251 題目大意: 首先給你一個圖,需要你求出最小生成樹,輸入N個節點,用大寫字母表示了節點,然後節點與節點之間有權

POJ 1251 Jungle Roads

sys them efi too ram cal hat lds space http://poj.org/problem?id=1251 The Head Elder of the tropical island of Lagrishan has a problem.

【Dijkstra演算法(鄰接+優先佇列優化) 建立虛點】HDU

Bessie and her friend Elsie decide to have a meeting. However, after Farmer John decorated his  fences they were separated into differen

Dijkstra(鄰接+優先佇列)

#include <iostream> #include<cstdio> #include<vector> #include<cstring> #include<queue> #include<algorit

Dijkstra[兩種鄰接+優先佇列優化]

Dijksta演算法中,如果我們採用的是鄰接矩陣來存的,第一點浪費的空間比較多,第二點我們知道演算法的時間複雜度在O(n*n),這樣的演算法可以說並不是很好,所以我們考慮優化它首先我們可以優化儲存結構,採用鄰接表來儲存,其次我們可以用優先佇列來排序大小,其時間複雜度大大降

POJ-1251-Jungle Roads

程序 pat sha ring urn main 因此 pac 方式 鏈接:https://vjudge.net/problem/POJ-1251 題意: 熱帶島嶼Lagrishan的頭長老有問題。幾年前,在村莊之間的額外道路上花了一大筆外援資金。但叢林無情地超越了道路

Silver Cow Party(短路 + Dijkstra + 鄰接 + 優先佇列

Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11348 Accepted: 5077 Description One cow from each of N farms (1 ≤ N 

dijsttra 鄰接+優先佇列

dijstra原理: 以單源開始,每次以新的點去更新所有的(沒訪問過的點) 到單源的最短路。        其中新的點---------應該找每次更新後當前到單源權值最小的點. 作法一:鄰接矩陣 void DIJ(int n)//傳入頂點個數n,預設0為起點 {

Dijkstra演算法(鄰接+優先佇列優化)

Dijkstra演算法是在圖論中應用很廣的一種演算法,它本質上是貪心的成功應用。它可以求加權圖的單源最短路。但是如果不優化的話,它的複雜度是O(n方),比較低效,一般我們採用鄰接表+優先佇列的優化。#include<bits/stdc++.h> using nam

Building RoadsPOJ - 3625】【Prime小生成樹

題目連結   一道簡單的MST(最小生成樹),但是一開始可能不在狀態,敲了個Kruskal,然後瘋狂RE,後來再想想,為什麼不把已經相連好的節點處理成點間距為0不就好了嘛。 #include <iostream> #include <cstdio>

BZOJ 1626 [Usaco2007 Dec]Building Roads 修建道路:kruskal(小生成樹

push_back spa pri family sca iostream 長度 con end 題目鏈接:http://www.lydsy.com/JudgeOnline/problem.php?id=1626 題意:   有n個農場,坐標為(x[i],y[i])。   

深度廣度優先遍歷小生成樹

怎麼用圖的深度和廣度優先遍歷來遍歷樹呢?我是這樣想的,把樹構造成圖就行了。 // 圖的遍歷.cpp : Defines the entry point for the console application. // #include "stdafx.h" #includ

Jungle Roads】【POJ - 1251】(小生成樹

題目: 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 s

POJ 2442 - Sequence - [小頂堆][優先佇列]

題目連結:http://poj.org/problem?id=2442 Time Limit: 6000MS Memory Limit: 65536K Description Given m sequences, each contains n non-negative integer. Now we

Running Median POJ - 3784 (對頂堆/優先佇列

For this problem, you will write a program that reads in a sequence of 32-bit signed integers. After each odd-indexed value is read, output the median (m

poj 1456 Supermarket 單純貪心||貪心+優先佇列||貪心+並查集

A supermarket has a set Prod of products on sale. It earns a profit px for each product x∈Prod sold by a deadline dx that is

poj 2274 線段樹+堆(優先佇列

The Race Time Limit: 15000MS Memory Limit: 65536K Total Submissions: 3685 Accepted: 756 Case Time Limit: 3000MS Description Durin