繼續暢通工程 hdu 1879

來源:互聯網
上載者:User

http://acm.hdu.edu.cn/showproblem.php?pid=1879

#include<iostream>//2243673 2010-03-24 20:56:41 Accepted 1879 375MS 320K 1070 B C++ 悔惜晟
#include<algorithm>//修改了三次終於AC, 為何就是代碼的效率不高 
#include<cstdio>
#include<cmath>

using namespace std;
int s[4991];
 
 struct stu
 {
  int x;
  int y;
  int dis;
 }df[4991];//開始這裡定義4591 也不知你是怎麼算
 
 int cmp(stu a, stu b)
 {
  return a.dis < b.dis;
 }
 
 int find(int a)
 {
  int r = a;
  while(r != s[r])
   r = s[r];
  return r;
 }
 
 void merge(int a, int b)
 {
  if(a > b)
   s[b] = a;
  else
   s[a] = b;
 }
 
int main()
{
 int n, a, b, c, d, sum, m;
 while(scanf("%d", &n)!=EOF && n!=0)
 {
  m = n*(n -1)/2;//這裡以開始也錯了n = 3 導致你錯的
  for(int i =0; i < m ; i++)
  {
   scanf("%d%d%d%d", &a, &b, &c, &d);
   if(d == 0)
   {
    df[i].x = a;
    df[i].y = b;
    df[i].dis = c;
   }
   else
   {
    
    df[i].x = a;
    df[i].y = b;
    df[i].dis = 0;
   
   }
  }
  sort(df, df + m, cmp);
  for(int i = 1; i <= n*(n-1)/2; i++)
   s[i]=i;
  sum =0;
  for(int i =0 ;i <n*(n-1)/2 ;i++)
  {
   int a = find(df[i].x);
   int b = find(df[i].y);
   if( a != b)
   {
    sum +=df[i].dis;
    merge(a, b);
    
   }
  }
  
  printf("%d/n",sum);
 }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.