zoukankan      html  css  js  c++  java
  • 7-10 公路村村通

    7-10 公路村村通(30 分)

    现有村落间道路的统计数据表中,列出了有可能建设成标准公路的若干条道路的成本,求使每个村落都有公路连通所需要的最低成本。

    输入格式:

    输入数据包括城镇数目正整数N(1000)和候选道路数目M(3N);随后的M行对应M条道路,每行给出3个正整数,分别是该条道路直接连通的两个城镇的编号以及该道路改建的预算成本。为简单起见,城镇从1到N编号。

    输出格式:

    输出村村通需要的最低成本。如果输入数据不足以保证畅通,则输出1,表示需要建设更多公路。

    输入样例:

    6 15
    1 2 5
    1 3 3
    1 4 7
    1 5 4
    1 6 2
    2 3 4
    2 4 6
    2 5 2
    2 6 6
    3 4 6
    3 5 1
    3 6 1
    4 5 10
    4 6 8
    5 6 3
    

    输出样例:

    12
    
     1 #include<iostream>
     2 #include<vector>
     3 using namespace std;
     4 #define maxNv 1001 
     5 #define maxcost 100000
     6 vector<int> cost(maxNv,maxcost);
     7 vector<int> parent(maxNv);//用来储存最小生成树 
     8 int flag=1;
     9 struct enode{
    10 int v1,v2;
    11 int weight;
    12 };
    13 using edge=enode*;
    14 struct graph{
    15 int Nv=0,Ne=0;
    16 int G[maxNv][maxNv];
    17 };
    18 using Graph=graph*;
    19 Graph CreateGraph(){
    20 Graph gra=new graph();
    21 cin>>gra->Nv>>gra->Ne;
    22 for(int i=0;i<maxNv;i++)
    23 for(int j=0;j<maxNv;j++)
    24 gra->G[i][j]=maxcost;
    25 return gra;
    26 }
    27 void Insert(Graph gra,edge e){
    28 gra->G[e->v1][e->v2]=e->weight;
    29 gra->G[e->v2][e->v1]=e->weight;
    30 }
    31 Graph BuildGraph(){
    32 Graph gra=CreateGraph();
    33 edge e=new enode();
    34 for(int i=0;i<gra->Ne;i++){
    35 cin>>e->v1>>e->v2>>e->weight;
    36 Insert(gra,e); 
    37 }
    38 return gra;
    39 }
    40 int findmin(Graph gra){
    41 int min=maxcost;
    42 int v=0;
    43 for(int i=1;i<=gra->Nv;i++)
    44 if(cost[i]!=0&&cost[i]<min) {min=cost[i];v=i;}
    45 return v;
    46 }
    47 void solve(Graph gra){
    48 int v1=1,v2=0;
    49 int mincost=0;
    50 cost[1]=0; 
    51 parent[1]=-1;
    52 for(v2=1;v2<=gra->Nv;v2++)
    53 if(cost[v2]!=0&&gra->G[v1][v2]!=maxcost&&gra->G[v1][v2]<cost[v2]){
    54 cost[v2]=gra->G[v1][v2];
    55 parent[v2]=v1;
    56 }
    57 while(1){
    58 v1=findmin(gra);
    59 if(v1==0) break;
    60 mincost+=cost[v1];
    61     cost[v1]=0;
    62 for(v2=1;v2<=gra->Nv;v2++)
    63 if(cost[v2]!=0&&gra->G[v1][v2]!=maxcost&&gra->G[v1][v2]<cost[v2]){
    64 cost[v2]=gra->G[v1][v2];
    65 parent[v2]=v1;
    66 }
    67 }
    68 for(int i=1;i<=gra->Nv;i++)
    69 if(cost[i]!=0) flag=0;//用来判断是否所有的点都被收进树中了 
    70 if(flag==0) cout<<-1;
    71 else
    72 cout<<mincost;
    73 }
    74 int main()
    75 {
    76   Graph gra=BuildGraph();
    77   solve(gra);
    78 /*  for(int i=1;i<=gra->Nv;i++){
    79   for(int j=1;j<=gra->Nv;j++)
    80   cout<<gra->G[i][j]<<" ";
    81   cout<<endl;
    82   }
    83 */
    84   return 0; 
    85 }
    View Code
     
     
  • 相关阅读:
    【整理】【代码的坏味道】过长函数(Long Method)
    【整理】【代码的坏味道】重复代码(Duplicated Code)
    【原创】Winform下拉框自动选择实现
    time及各种cpu时间
    arch安装及配置xfce4桌面
    paste工具
    十分有用的cut剪切命令
    ubuntu一些脚本的执行顺序
    Linux一些经典书籍
    强大的wget下载工具
  • 原文地址:https://www.cnblogs.com/A-Little-Nut/p/8056234.html
Copyright © 2011-2022 走看看