HDU 1879 繼續暢通工程

2021-09-23 07:45:42 字數 1090 閱讀 5199

本題是典型的最小生成樹,用到並查集知識,所以使用並查集實現的克魯斯卡爾演算法

先將修好的路進行連通,在計算**時自動忽略已經建好路的**

本題做的時候,一直超時,原因在於用 cin 讀入資料時,耗時太大,導致不能ac,最後改為用 scanf 快多了

#include #include #include using namespace std;

const int maxn = 50 * 99 + 5;

int pre[maxn];

struct infor s[maxn];

bool cmp(infor s1, infor s2)

int find(int x)

return r;

}void join(int x, int y)

int main()

for (int i = 0; i < m; i++)

} int sum = 0;

sort(s, s + m, cmp);//按照**排序

for (int i = 0; i < m; i++)

} cout << sum << endl;

} return 0;

}

#include #include #include #include using namespace std;

const int maxn = 5000;

int pre[maxn];

struct infor

}s[maxn];

int find(int x)

void join(int x, int y)

int main()

for(int i = 0; i < m; i++)

}int sum = 0;

sort(s, s + m);

for(int i = 0; i < m; i++)

}printf("%d\n",sum);

//cout << sum << endl;

}return 0;

}

繼續暢通工程 hdu 1879

include 2243673 2010 03 24 20 56 41 accepted 1879 375ms 320k 1070 b c 悔惜晟 include 修改了三次終於ac,為何就是 的效率不高 include include using namespace std int s 4991 ...

hdu 1879 繼續暢通工程

include include include typedef structedge edge input 5050 int cost 5050 int parent 5050 int cmp const void a,const void b int root int n int kruskal ...

HDU 1879 繼續暢通工程

和前幾個最小生成樹有點不一樣的地方就是 在kruskal裡面,要先把已經修通的路排在前面 不知道這樣是不是多此一舉呢 再按照每條路的價值從小到大排序 自定義排序規則就是 呼叫c 的sort int cmp const e a,const e b 將每條邊存在結構體edge裡面 struct e ed...