[sicily online]1083. Networking

來源:互聯網
上載者:User
/*輸入進行處理的最小產生樹(我用的是prim演算法)ConstraintsTime Limit: 1 secs, Memory Limit: 32 MBDescriptionYou are assigned to design network connections between certain points in a wide area. You are given a set of points in the area, and a set of possible routes for the cables that may connect pairs of points. For each possible route between two points, you are given the length of the cable that is needed to connect the points over that route. Note that there may exist many possible routes between two given points. It is assumed that the given possible routes connect (directly or indirectly) each two points in the area.Your task is to design the network for the area, so that there is a connection (direct or indirect) between every two points (i.e., all the points are interconnected, but not necessarily by a direct cable), and that the total length of the used cable is minimal.InputThe input file consists of a number of data sets. Each data set defines one required network. The first line of the set contains two integers: the first defines the number P of the given points, and the second the number R of given routes between the points. The following R lines define the given routes between the points, each giving three integer numbers: the first two numbers identify the points, and the third gives the length of the route. The numbers are separated with white spaces. A data set giving only one number P=0 denotes the end of the input. The data sets are separated with an empty line.The maximal number of points is 50. The maximal length of a given route is 100. The number of possible routes is unlimited. The nodes are identified with integers between 1 and P (inclusive). The routes between two points i and j may be given as i j or as j i.OutputFor each data set, print one number on a separate line that gives the total length of the cable used for the entire designed network.Sample Input1 02 31 2 372 1 171 2 683 71 2 192 3 113 1 71 3 52 3 893 1 911 2 325 71 2 52 3 72 4 84 5 113 5 101 5 64 2 120Sample Output0171626*/#include<iostream>#include <iomanip>#include<stdio.h>#include<cmath>#include<iomanip>#include<list>#include <map>#include <vector>#include <string>#include <algorithm>#include <sstream>#include <stack>#include<queue>#include<string.h>using namespace std;typedef struct NODE{bool flag;map<int,int> neighbour;}node;int main(){int n;unsigned long long m;while(cin>>n>>m&&n!=0){vector<node> gra(n);for(int i=0;i<n;i++)gra[i].flag=false;for(int xx=0;xx<m;xx++){int i,j,value;cin>>i>>j>>value;i--;j--;map<int,int>::iterator ite=gra[i].neighbour.find(j);if(ite==gra[i].neighbour.end()){gra[i].neighbour.insert(make_pair(j,value));gra[j].neighbour.insert(make_pair(i,value));}else {if(value<ite->second){gra[i].neighbour[j]=value;gra[j].neighbour[i]=value;}}}//end forint count=1;vector<int> jihe;jihe.push_back(0);gra[0].flag=true;int sum=0;while(count<n){int min=999999;int minIndex=0;for(vector<int>::size_type i=0;i<jihe.size();i++){for(map<int,int>::iterator j=gra[jihe[i]].neighbour.begin();j!=gra[jihe[i]].neighbour.end();j++)//記得是jihe[i]{if(gra[j->first].flag==false&&min>j->second){minIndex=j->first;min=j->second;}}}sum+=min;gra[minIndex].flag=true;jihe.push_back(minIndex);count++;}cout<<sum<<endl;}//end while}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.