zoukankan      html  css  js  c++  java
  • HDU_1233_还是畅通工程

    还是畅通工程

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 28842    Accepted Submission(s): 12893


    Problem Description
    某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离。省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可),并要求铺设的公路总长度为最小。请计算最小的公路总长度。
     
    Input
    测试输入包含若干测试用例。每个测试用例的第1行给出村庄数目N ( < 100 );随后的N(N-1)/2行对应村庄间的距离,每行给出一对正整数,分别是两个村庄的编号,以及此两村庄间的距离。为简单起见,村庄从1到N编号。
    当N为0时,输入结束,该用例不被处理。
     
    Output
    对每个测试用例,在1行里输出最小的公路总长度。
     
    Sample Input
    3 1 2 1 1 3 2 2 3 4 4 1 2 1 1 3 4 1 4 1 2 3 3 2 4 2 3 4 5 0
     
    Sample Output
    3 5
     
    题解:并查集问题。用结构体来表示每一条路的起点、终点和距离。用贪心的思想将其从小到大排序,再由距离最短的路开始连通,合并集合前判断是否会成环,若不能成环则sum    加上这条边的值。
     

     代码:

    #include<stdio.h>
    struct path
    {
      int a;  //a地
      int b;   //b地
      int fee;  //距离
    };
    struct path num[10000];
    int father[10000],sum;
    void sort(struct path num[],int n)    //排序
    {
      struct path term;
      int i,j,flag;
      for(i=0; i<n; i++)
      {
        flag=i;
        for(j=i+1; j<n; j++)
          if(num[j].fee<num[flag].fee)
            flag=j;
        term=num[i];
        num[i]=num[flag];
        num[flag]=term;
      }
    }

    int find(int a)
    {
      if(father[a]!=a)
        father[a]=find(father[a]);
      return father[a];
    }

    void merge(struct path pre)
    {
      int x,y;
      x=find(pre.a);
      y=find(pre.b);
      if(x!=y)
      {
        father[x]=y;
        sum+=pre.fee;
      }
     }
    int main()
    {
      int n,i;
      while(1)
      {
        scanf("%d",&n);
        if(n==0)
          break;
        for(i=1; i<=n; i++)
          father[i]=i;
        for(i=0; i<n*(n-1)/2; i++)
          scanf("%d%d%d",&num[i].a,&num[i].b,&num[i].fee);
        sort(num,n*(n-1)/2);
        sum=0;
        for(i=0; i<n*(n-1)/2; i++)
          merge(num[i]);
        printf("%d ",sum);
      }
      return 0;
    }

  • 相关阅读:
    How to implement long running flows, sagas, business processes or similar
    What are long running processes?
    The Microservices Workflow Automation Cheat Sheet
    cget cmake 包管理工具
    buckaroo 试用
    buckaroo 去中心化的c++包管理工具
    What's New In Zeebe: Scaling Zeebe, New Client APIs, Faster Requests, Timestamps, NodeJS Client, and Default Topic is Back!
    Benchmarking Zeebe: An Intro to How Zeebe Scales Horizontally and How We Measure It
    Architecture options to run a workflow engine
    camunda 开源的bpm系统
  • 原文地址:https://www.cnblogs.com/jasonlixuetao/p/4369814.html
Copyright © 2011-2022 走看看