hdu 3371 最小生成树 prim

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

题目大意:告诉你有几座城市,再告诉你哪两座城市之间建路要多少钱,在给你哪几个城市之间已经有路,不需要再建。要求的是要使所有城市之间连通最小要花费多少钱。

这里我用了prim算法。。保存城市之间的权值,对于已经建好的城市,将他们的权值赋为0。还有就是要判断是否能找出最小生成树,如果不可以就输出-1,如果在遍历点的时候,并不能找出一个最小的边,那么肯定就是没有最小生成树。(注意要在每个kase开始的时候将所有的边都初始化为INF)。

贡献了好几次WA。。。因为脑残的在每个kase里面也打了一个while,一直退不出来。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define M 1009
#define INF 0x3f3f3f3f
int dis[M];
int map[M][M];
bool vis[M];
int n;
int ans;
int city[M];
bool prim()
{
    for(int i = 1;i <= n;i++)
        dis[i] = map[1][i];
    vis[1] = true;
    for(int i = 2;i <= n;i++)
    {
        int min = INF;
        int k;
        for(int j = 1;j <= n;j++)
        {
            if(!vis[j] && dis[j]<min)
            {
                min = dis[j];
                k = j;
            }
        }
        if(min==INF) return false; //判断是否有这个最小生成树,如果找不出这个最小边就意味着没有
        ans += min;
        vis[k] = true;
        for(int j = 1;j <= n;j++)
        {
            if(!vis[j] && dis[j]>map[k][j])
            {
                dis[j] = map[k][j];
            }
        }
    }
    return true;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        ans = 0;
        memset(vis,0,sizeof(vis));
        memset(city,0,sizeof(city));
        int m,k;
        scanf("%d%d%d",&n,&m,&k);   //一开始打成while(scanf(...))  被反馈一个WA 找了好久
        for(int i = 1;i <= n;i++)
            for(int j = 1;j <= n;j++)
        {
            map[i][j] = INF;
            map[j][i] = INF;
        }
        for(int 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(int i = 1;i <= k;i++)
        {
            int temp;
            scanf("%d",&temp);
            for(int i = 1;i <= temp;i++)
                scanf("%d",&city[i]);//保存已经连通的城市
            for(int i = 1;i <= temp;i++)
            for(int j = i+1;j <= temp;j++)
            {
                int a = city[i],b = city[j];
                map[a][b] = 0;
                map[b][a] = 0;
            }
        }
        bool ok = prim();
        if(!ok)
            printf("-1\n");
        else
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-10-12 04:39:53

hdu 3371 最小生成树 prim的相关文章

hdu 3371 最小生成树prim算法

Connect the Cities Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8992    Accepted Submission(s): 2519 Problem Description In 2100, since the sea level rise, most of the cities disappear. Thoug

hdu 1301 最小生成树prim实现

http://acm.hdu.edu.cn/showproblem.php?pid=1301 Jungle Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4145    Accepted Submission(s): 3020 Problem Description The Head Elder of the tropica

HDU 3371 kruscal/prim求最小生成树 Connect the Cities 大坑大坑

这个时间短 700多s #include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> using namespace std; struct node{ int u; int v; int w; }que[100000]; int father[505]; bool cmp(struct node a,struct node b){ return a.w<b.w;

hdu 3371(prim算法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3371 Connect the Cities Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 10313    Accepted Submission(s): 2937 Problem Description In 2100, since th

hdu 1233(最小生成树 prim算法)

还是畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 43080    Accepted Submission(s): 19636 Problem Description 某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离.省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相

hdu 3371 最小生成树

深夜来一发,裸的最小生成树,数据量还挺大,kruskal算法g++始终过不去,c++刚好飘过,这个时候就体现出kruskal和prim的适用范围的不同了,前者适用于稀疏图,后者适用于稠密图. kruskal算法: 1 #include <stdio.h> 2 #include <algorithm> 3 using namespace std; 4 5 const int N = 501; 6 const int M = 25000; 7 int f[N]; 8 int buffe

HDU 1162 Eddy&#39;s picture (最小生成树 prim)

题目链接 Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to become a painter.Every day Eddy draws pictures in his small room, and he usually puts out his newest pictures to let his friends appreciate. but the res

hdu 3371 Connect the Cities

链接:hdu 3371 已知已连通的路的序号,以及未连通的路的费用,求将所有城市连通的最小费用 也是将已连通的路的费用记为0,就转化成了基本最小生成树的题 不过这题数组要开的大点,不然很容易就RE了... #include<cstdio> #include<algorithm> using namespace std; int f[510],n,m; struct stu { int a,b,c; }t[100000]; int cmp(struct stu x,struct st

poj1861 最小生成树 prim &amp; kruskal

// poj1861 最小生成树 prim & kruskal // // 一个水题,为的只是回味一下模板,日后好有个照应不是 #include <cstdio> #include <algorithm> #include <cstring> #include <vector> #include <iostream> using namespace std; const int MAX_N = 1008; const int INF =