(hdu step 6.1.7)Connect the Cities(在有的路已經修建好的情況下,求讓n個點連通的最小費用),hdu6.1.7
題目:
| Connect the Cities |
| Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) |
| Total Submission(s): 391 Accepted Submission(s): 139 |
| |
| Problem DescriptionIn 2100, since the sea level rise, most of the cities disappear. Though some survived cities are still connected with others, but most of them become disconnected. The government wants to build some roads to connect all of these cities again, but they don’t want to take too much money. |
InputThe first line contains the number of test cases.
Each test case starts with three integers: n, m and k. n (3 <= n <=500) stands for the number of survived cities, m (0 <= m <= 25000) stands for the number of roads you can choose to connect the cities and k (0 <= k <= 100) stands for the number of still connected cities.
To make it easy, the cities are signed from 1 to n.
Then follow m lines, each contains three integers p, q and c (0 <= c <= 1000), means it takes c to connect p and q.
Then follow k lines, each line starts with an integer t (2 <= t <= n) stands for the number of this connected cities. Then t integers follow stands for the id of these cities.
|
| OutputFor each case, output the least money you need to take, if it’s impossible, just output -1. |
Sample Input
16 4 31 4 22 6 12 3 53 4 332 1 22 1 33 4 5 6 |
Sample Output
1 |
| Authordandelion |
| SourceHDOJ Monthly Contest – 2010.04.04 |
| Recommendlcy |
題目分析:
求最小產生樹。這道題用kruscal來做會TLE。畢竟kruscal適用於稀疏圖,這道題的便可能會比較多一點。然後改用prim來做,用C++提交就能AC了(杭電的伺服器感覺最近是不是有點。。。同樣的代碼第一次交是TLE(>1000ms),第二次交就493ms,這是不是差的略多一點。這會讓人崩潰的。。。)。
需要注意一下的是這道題關於“已建路”的輸入資料格式:
例如範例中的:
16 4 31 4 22 6 12 3 53 4 332 1 22 1 33 4 5 6
從“2 1 2”開始就是已建路的資料。其意思為點1與點2之間的路已經建好。後面的意思是點1與點3的路已經建好,
在後面的就是點4 與點5 與點6之間連通。
代碼如下:
/* * g1.cpp * * Created on: 2015年3月10日 * Author: Administrator */#include <iostream>#include <cstdio>#include <string>using namespace std;const int maxn = 501;const int inf = 999999;int map[maxn][maxn];bool p[maxn];int dis[maxn];int pre[maxn];int n;int prim(){int sum = 0;int i;for(i = 2 ; i <= n ; ++i){p[i] = false;dis[i] = map[1][i];pre[i] = 1;}dis[1] = 0;p[1] = true;pre[1] = 1;for(i = 1 ; i <= n-1 ; ++i){int min = inf;int k = 0;int j;for(j = 1 ; j <= n ; ++j){if(!p[j] && dis[j] < min){//dis[j] = min;min = dis[j];k = j;}}if(k == 0){//沒有點可以拓展,圖不連通,無法求最小產生樹return -1;//直接返回-1.}sum += dis[k];p[k] = true;for(j = 1 ; j <= n ; ++j){if(!p[j] && dis[j] > map[k][j]){dis[j] = map[k][j];pre[j] = k;}}}return sum;}int main(){int t;scanf("%d",&t);int tmp[maxn];while(t--){int m,k;scanf("%d%d%d",&n,&m,&k);int i,j;for(i = 1 ; i <= n ; ++i){for(j = 1 ; j <= n ; ++j){if(i == j){map[i][j] = 0;}else{map[i][j] = inf;}}}for(i = 1 ; i <= m ; ++i){int a,b,c;scanf("%d%d%d",&a,&b,&c);if(map[a][b] > c){map[a][b] = map[b][a] = c;}}//對於已經建好的路的資料的處理.(需要注意一下,跟之前的題目給出的形式不太一樣)for(i = 1 ; i <= k ; ++i){int size;scanf("%d",&size);//int tmp[size];for(j = 0 ; j < size ; ++j){scanf("%d",&tmp[j]);}for(j = 0 ; j < size-1 ; ++j){map[tmp[j]][tmp[j+1]] = 0;map[tmp[j+1]][tmp[j]] = 0;}}printf("%d\n",prim());//printf("%d\n",prim1());}return 0;}
以下是用kruscal來做的TLE的版本:
/* * g.cpp * * Created on: 2015年3月10日 * Author: Administrator */#include <iostream>#include <cstdio>#include <algorithm>#include <vector>using namespace std;const int maxn = 505;struct Edge{int begin;int end;int weight;}edges[maxn*maxn];int father[maxn];int n;int find(int a){if(a == father[a]){return a;}return father[a] = find(father[a]);}int kruscal(int count){int i;for(i = 1 ; i <= n ; ++i){father[i] = i;}int counter = 0;int sum = 0;for(i = 1 ; i <= count ; ++i){if(counter == n-1){break;}int fa = find(edges[i].begin);int fb = find(edges[i].end);if(fa != fb){father[fa] = fb;sum += edges[i].weight;counter++;}}//printf("%d\n",counter);return sum;}bool cmp(Edge a,Edge b){return a.weight < b.weight;}int main(){int t;scanf("%d",&t);int tmp[maxn];while(t--){int m,k;scanf("%d%d%d",&n,&m,&k);int cnt = 1;int i;for(i = 1 ; i <= m ; ++i){scanf("%d%d%d",&edges[cnt].begin,&edges[cnt].end,&edges[cnt++].weight);}for(i = 1 ; i <= k ; ++i){ int size;scanf("%d",&size);//int tmp[size];//vector<int> tmp;int j;for(j = 0 ; j < size ; ++j){scanf("%d",&tmp[j]);}for(j = 0 ; j < size-1 ; ++j){edges[cnt].begin = tmp[j];edges[cnt].end = tmp[j+1];edges[cnt++].weight = 0;}}cnt -= 1;sort(edges+1,edges+1+cnt,cmp);printf("%d\n",kruscal(cnt));}return 0;}
如果對prim演算法不太熟悉的可以拿這道題練習一下。它是一道求最小產生樹的裸題:
http://www.sdutacm.org/sdutoj/problem.php?action=showproblem&problemid=2144
這道題的AC代碼是:
/* * t.cpp * * Created on: 2015年3月10日 * Author: Administrator */#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int maxn = 101;const int inf = 99999;int map[maxn][maxn];bool p[maxn];int dis[maxn];int pre[maxn];int n,m;int prim(){int sum = 0;int i;for(i = 2 ; i <= n ; ++i){p[i] = false;dis[i] = map[1][i];pre[i] = 1;}p[1] = true;dis[1] = 0;for(i = 1 ; i <= n-1 ; ++i){int min = inf;int k = 0;int j;for(j = 1 ; j <= n ; ++j){if(!p[j] && dis[j] < min){min = dis[j];k = j;}}if(k == 0){return -10;}sum += dis[k];p[k] = true;for(j = 1 ; j <= n ; ++j){if(!p[j] && dis[j] > map[k][j]){dis[j] = map[k][j];pre[j] = k;}}}return sum;}int main(){while(scanf("%d%d",&n,&m)!=EOF){//memset(map,inf,sizeof(map));memset(p,false,sizeof(p));int i;int j;for(i = 1 ; i <= n ; ++i){for(j = 1 ; j <= n ; ++j){if(i == j){map[i][j] = 0;}else{map[i][j] = inf;}}}for(i = 1 ; i <= m ; ++i){int a,b,c;scanf("%d%d%d",&a,&b,&c);if(map[a][b] > c){map[a][b] = map[b][a] = c;}}printf("%d\n",prim());}return 0;}