《算法筆記》10.5小節——圖算法專題->最小生成樹 問題 E: 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.

樣例輸入
3
A 1 B 42
B 1 C 87
6
A 2 B 13 E 55
B 1 C 1
C 1 D 20
D 1 E 4
E 1 F 76
0
樣例輸出
129
114

題目大意:給出 n 個城市,之后給出 n-1 行信息,第一個字符表示城市的編號 c,之后第一個數字 cnt 表示該城市出發有幾條路徑。之后給出 cnt 組信息,每組一個字符一個數字,表示從城市 c 到對應城市的長度。求整個圖的最小生成樹的總長度。

分析:最小生成樹模板題。由于給出的是邊信息,可以用 kruskal 算法。

#include<algorithm>
#include <iostream>
#include  <cstdlib>
#include  <cstring>
#include   <string>
#include   <vector>
#include   <cstdio>
#include    <queue>
#include    <stack>
#include    <ctime>
#include    <cmath>
#include      <map>
#include      <set>
#define INF 0x3fffffff
#define db1(x) cout<<#x<<"="<<(x)<<endl
#define db2(x,y) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<endl
#define db3(x,y,z) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<endl
#define db4(x,y,z,r) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#r<<"="<<(r)<<endl
#define db5(x,y,z,r,w) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#r<<"="<<(r)<<", "<<#w<<"="<<(w)<<endl
using namespace std;typedef struct node
{int t,dis;
}node;int findFather(int a,int father[])
{int z=a;while(a!=father[a])a=father[a];while(z!=a){int temp=father[z];father[z]=a,z=temp;}return a;
}int kruskal(vector<node>graph[],int n,int father[])
{int ans=0;for(int times=1;times<n;++times){int u=-1,v=-1,mini=INF;for(int i=0;i<n;++i){int len=graph[i].size();int fi=findFather(i,father);for(int j=0;j<len;++j){if(graph[i][j].dis<mini){int fj=findFather(graph[i][j].t,father);if(fi!=fj)u=i,v=j,mini=graph[i][j].dis;}}}if(u==-1)return -1;ans+=graph[u][v].dis;int fu=findFather(u,father);int fv=findFather(graph[u][v].t,father);father[fu]=fv;}return ans;
}int main(void)
{#ifdef testfreopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);clock_t start=clock();#endif //testint n;while(scanf("%d\n",&n),n){vector<node>graph[n+5];int father[n+5];for(int i=0;i<n;++i)father[i]=i;for(int t=1;t<n;++t){int cnt;char c;scanf("%c%d ",&c,&cnt);int index=c-'A';for(int i=0;i<cnt;++i){int d;char cc;scanf("%c%d",&cc,&d);getchar();node temp;temp.t=cc-'A',temp.dis=d;graph[index].push_back(temp);}}int ans=kruskal(graph,n,father);printf("%d\n",ans);}#ifdef testclockid_t end=clock();double endtime=(double)(end-start)/CLOCKS_PER_SEC;printf("\n\n\n\n\n");cout<<"Total time:"<<endtime<<"s"<<endl;        //s為單位cout<<"Total time:"<<endtime*1000<<"ms"<<endl;    //ms為單位#endif //testreturn 0;
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/diannao/81044.shtml
繁體地址,請注明出處:http://hk.pswp.cn/diannao/81044.shtml
英文地址,請注明出處:http://en.pswp.cn/diannao/81044.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

【音視頻】SDL簡介

官網&#xff1a;官網 文檔&#xff1a;文檔 SDL&#xff08;Simple DirectMedia Layer&#xff09;是一套開放源代碼的跨平臺多媒體開發庫&#xff0c;使用C語言寫成。SDL提供數種控制圖像、聲音、輸出入的函數&#xff0c;讓開發者只 要用相同或是相似的代碼就可以開發出跨多…

SpringBoot + SSE 實時異步流式推送

前言 在當今數字化時代&#xff0c;實時數據處理對于企業的決策和運營至關重要。許多業務場景需要及時響應數據庫中的數據變化&#xff0c;例如電商平臺實時更新庫存、金融系統實時監控交易數據等。 本文將詳細介紹如何通過Debezium捕獲數據庫變更事件&#xff0c;并利用Serv…

ADS1299模擬前端(AFE)代替芯片——LHE7909

在現代醫療科技的飛速發展中&#xff0c;精確的生物電勢測量設備變得越來越重要。領慧立芯推出的LHE7909&#xff0c;是一款專為心電圖&#xff08;ECG&#xff09;和其他生物電勢測量設計的低噪聲24位模數轉換器&#xff08;ADC&#xff09;&#xff0c;為醫療設備制造商提供了…

如何實現Redis和Mysql中數據雙寫一致性

一、引言 今天我們來聊聊一個在分布式系統中非常常見但又十分棘手的問題——Redis與MySQL之間的雙寫一致性。我們在項目中多多少少都遇到過類似的困擾&#xff0c;緩存是用Redis&#xff0c;數據庫是用MySQL&#xff0c;但如何確保兩者之間的數據一致性呢&#xff1f;接下來我…

面試篇 - Transformer前饋神經網絡(FFN)使用什么激活函數?

1. FFN結構分解 原始Transformer的FFN層 FFN(x) max(0, xW? b?)W? b? # 原始論文公式 輸入&#xff1a;自注意力層的輸出 x&#xff08;維度 d_model512&#xff09; 擴展層&#xff1a;xW? b?&#xff08;擴展為 d_ff2048&#xff09; 激活函數&#xff1a;Re…

基于Python Flask的深度學習電影評論情感分析可視化系統(2.0升級版,附源碼)

博主介紹&#xff1a;?IT徐師兄、7年大廠程序員經歷。全網粉絲15W、csdn博客專家、掘金/華為云//InfoQ等平臺優質作者、專注于Java技術領域和畢業項目實戰? &#x1f345;文末獲取源碼聯系&#x1f345; &#x1f447;&#x1f3fb; 精彩專欄推薦訂閱&#x1f447;&#x1f3…

前端vue2修改echarts字體為思源黑體-避免侵權-可以更換為任意字體統一管理

1.下載字體 npm install fontsource/noto-sans-sc 不知道為什么我從github上面下載的不好使&#xff0c;所以就用了npm的 2.引用字體 import fontsource/noto-sans-sc; 在入口文件-main.js中引用 3.設置echats模板樣式 import * as echarts from echarts; // 在import的后…

51c自動駕駛~合集37

我自己的原文哦~ https://blog.51cto.com/whaosoft/13878933 #DETR->DETR3D->Sparse4D 走向長時序稀疏3D目標檢測 一、DETR 圖1 DETR架構 DETR是第一篇將Transformer應用到目標檢測方向的算法。DETR是一個經典的Encoder-Decoder結構的算法&#xff0c;它的骨干網…

【MongoDB篇】MongoDB的集合操作!

目錄 引言第一節&#xff1a;集合的“誕生”——自動出現還是手動打造&#xff1f;&#x1f914;第二節&#xff1a;集合的“查閱”——看看這個數據庫里有哪些柜子&#xff1f;&#x1f4c2;&#x1f440;第三節&#xff1a;集合的“重命名”——給文件柜換個名字&#xff01;…

Goland終端PowerShell命令失效

Goland終端Terminal的PowerShell不能使用&#xff0c;明明windows上升級了PowerShell 7設置了配置文件&#xff0c;但是只能在windows終端下使用&#xff0c;goland終端下直接失效報錯&#xff0c;安裝升級PowerShell請看Windows11終端升級PowerShell7 - HashFlag - 博客園 問…

簡單分析自動駕駛發展現狀與挑戰

一、技術進展與市場滲透 技術分級與滲透率 當前量產乘用車的自動駕駛等級以L2為主&#xff08;滲透率約51%&#xff09;&#xff0c;L3級處于初步落地階段&#xff08;滲透率約20%&#xff09;&#xff0c;而L4級仍處于測試和示范運營階段&#xff08;滲透率約11%&#xff09;2…

【C++類和數據抽象】消息處理示例(1):從設計模式到實戰應用

目錄 一、數據抽象概述 二、消息處理的核心概念 2.1 什么是消息處理&#xff1f; 2.2 消息處理的核心目標 三、基于設計模式的消息處理實現 3.1 觀察者模式&#xff08;Observer Pattern&#xff09; 3.2 命令模式&#xff08;Command Pattern&#xff09; 四、實戰場景…

【統計方法】交叉驗證:Resampling, nested 交叉驗證等策略 【含R語言】

Resampling (重采樣方法) 重采樣方法是從訓練數據中反復抽取樣本&#xff0c;并在每個&#xff08;重新&#xff09;樣本上重新調整模型&#xff0c;以獲得關于擬合模型的附加信息的技術。 兩種主要的重采樣方法 Cross-Validation (CV) 交叉驗證 &#xff1a; 用于估計測試誤…

常見的 CSS 知識點整理

1. 盒模型&#xff08;Box Model&#xff09;是什么&#xff1f;標準盒模型和 IE 盒模型的區別&#xff1f; 答案&#xff1a; CSS 盒模型將元素視為一個盒子&#xff0c;由內容&#xff08;content&#xff09;、內邊距&#xff08;padding&#xff09;、邊框&#xff08;bor…

Educational Codeforces Round 178 div2(題解ABCDE)

A. Three Decks #1.由于最后三個數會相等&#xff0c;提前算出來和&#xff0c;%3判斷&#xff0c;再判前兩個數是否大于 #include<iostream> #include<vector> #include<stdio.h> #include<map> #include<string> #include<algorithm> #…

如何創建一個導入模板?全流程圖文解析

先去找到系統內可以上傳東西的按鈕 把你的模板上傳上去,找到對應的fileName 圖里的文字寫錯了,是復制粘貼"filePath"到URL才能下載

通信原理第七版與第六版區別附pdf

介紹 我用夸克網盤分享了「通信原理 第7版》樊昌信」&#xff0c;鏈接&#xff1a;https://pan.quark.cn/s/be7c5af4cdce 《通信原理&#xff08;第7版&#xff09;》是在第6版的基礎上&#xff0c;為了適應當前通信技術發展和教學需求&#xff0c;并吸取了數十所院校教師的反…

Mysql唯一性約束

唯一性約束&#xff08;Unique Constraint&#xff09;是數據庫設計中用于保證表中某一列或多列組合的值具有唯一性的一種規則。它可以防止在指定列中插入重復的數據&#xff0c;有助于維護數據的完整性和準確性。下面從幾個方面為你詳細解釋 作用 確保數據準確性&#xff1a…

測試基礎筆記第十六天

提示&#xff1a;文章寫完后&#xff0c;目錄可以自動生成&#xff0c;如何生成可參考右邊的幫助文檔 文章目錄 一、UI自動化介紹1.認識UI自動化測試2.實施UI自動化測試前置條件3.UI自動化測試執行時機4.UI自動化測試核心作用和劣勢 二、認識Web自動化測試工具-Selenium021.Sel…

PaddleX的安裝

參考&#xff1a;安裝PaddlePaddle - PaddleX 文檔 1、安裝PaddlePaddle 查看 docker 版本 docker --version 若您通過 Docker 安裝&#xff0c;請參考下述命令&#xff0c;使用飛槳框架官方 Docker 鏡像&#xff0c;創建一個名為 paddlex 的容器&#xff0c;并將當前工作目…