__周赛(最小生成树(Prime))

将已经链接的边的权值设为0即可。

但是可能会超时,提交的时候,有一次显示超时,所以这个解法是有问题的,看到有171ms的,实力差的太大了,还是得使劲刷题。

/*2015-5-18 951ms*/#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

#define INF 0x3f3f3f3f

int map[510][510],p[510],n;

int prime(){

    int visit[510];
    int less[510];

    int nodes,sum;

    sum = 0;nodes = 1;

    memset(visit,0,sizeof(visit));

    visit [1] = 1;

    for(int i = 1;i<=n;i++){

        less[i] = map[1][i];
    }
    int temp,k;
    for(int i = 1;i<=n;i++){

        temp = INF;
        for(int j = 1;j<=n;j++){
            if(!visit[j] && temp > less[j])
                temp = less[k=j];
        }
        if(temp == INF)break;
        nodes++;
        sum  += less[k];

        visit[k] = 1;

        for(int j = 1;j<=n;j++){
            if(!visit[j] && less[j] > map[k][j])
                less[j] = map[k][j];
        }

    }
//    printf("%d\n",nodes);
    if(nodes == n)return sum;
    else return -1; 

}

int main() {

    int t ;

    scanf("%d",&t);

    while(t--){

        int m,k;

        memset(map,INF,sizeof(map));

        scanf("%d%d%d",&n,&m,&k);

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

            int a,b,c;

            scanf("%d%d%d",&a,&b,&c);

            map[a][b] = map[b][a] = min(map[a][b],c);

        }
        for(int i = 0;i<k;i++){
            int d;

            scanf("%d",&d);

            int x[510];

            for(int j =0;j<d;j++){

                scanf("%d",&x[j]);

            }
            for(int j = 0;j<d-1;j++)
              for(int jj = j+1;jj<d;jj++)
                  map[x[j]][x[jj]] = map[x[jj]][x[j]] = 0;
        }
        printf("%d\n",prime());

    }

}
  1 /*这个代码还是比较给力的,这个是利用了Prime算法的特点,将连起来的点构成链,2015-5-18 717ms*/
  2 #include<stdio.h>
  3 #include<string.h>
  4 #include<algorithm>
  5 using namespace std;
  6
  7 #define INF 0x3f3f3f3f
  8
  9 int map[510][510],p[510],n;
 10
 11
 12 int prime(){
 13
 14     int visit[510];
 15     int less[510];
 16
 17     int nodes,sum;
 18
 19     sum = 0;nodes = 1;
 20
 21     memset(visit,0,sizeof(visit));
 22
 23     visit [1] = 1;
 24
 25     for(int i = 1;i<=n;i++){
 26
 27         less[i] = map[1][i];
 28     }
 29     int temp,k;
 30     for(int i = 1;i<=n;i++){
 31
 32         temp = INF;
 33         for(int j = 1;j<=n;j++){
 34             if(!visit[j] && temp > less[j])
 35                 temp = less[k=j];
 36         }
 37         if(temp == INF)break;
 38         nodes++;
 39         sum  += less[k];
 40
 41         visit[k] = 1;
 42
 43         for(int j = 1;j<=n;j++){
 44             if(!visit[j] && less[j] > map[k][j])
 45                 less[j] = map[k][j];
 46         }
 47
 48     }
 49 //    printf("%d\n",nodes);
 50     if(nodes == n)return sum;
 51     else return -1;
 52
 53 }
 54
 55
 56 int main() {
 57
 58     int t ;
 59
 60     scanf("%d",&t);
 61
 62     while(t--){
 63
 64         int m,k;
 65
 66         memset(map,INF,sizeof(map));
 67
 68         scanf("%d%d%d",&n,&m,&k);
 69
 70         for(int i = 0;i<m;i++){
 71
 72             int a,b,c;
 73
 74             scanf("%d%d%d",&a,&b,&c);
 75
 76             map[a][b] = map[b][a] = min(map[a][b],c);
 77
 78         }
 79         for(int i = 0;i<k;i++){
 80             int d;
 81
 82             scanf("%d",&d);
 83
 84             int x,last;
 85
 86             scanf("%d",&last);
 87             for(int j =2;j<=d;j++){
 88
 89                 scanf("%d",&x);
 90                 map[x][last] = map[last][x] = 0;
 91                 last = x;
 92             }
 93
 94         }
 95         printf("%d\n",prime());
 96
 97     }
 98
 99
100
101 }
时间: 2024-10-12 16:25:51

__周赛(最小生成树(Prime))的相关文章

最小生成树 prime poj1258

题意:给你一个矩阵M[i][j]表示i到j的距离 求最小生成树 思路:裸最小生成树 prime就可以了 最小生成树专题 AC代码: #include "iostream" #include "string.h" #include "stack" #include "queue" #include "string" #include "vector" #include "set&

poj1258 Agri-Net +hdu 1233 还是畅通工程 (最小生成树Prime算法)

Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 43215   Accepted: 17683 Description Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He nee

hdu 1875 最小生成树 prime版

最小生成树prime版 大致的步骤 首先选取一个到集合最近的点 然后标记起在集合内部 然后更新最短距离 畅通工程再续 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 24846    Accepted Submission(s): 8035 Problem Description 相信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不

POJ - 1258(最小生成树.prime)

题目链接: http://poj.org/problem?id=1258 题目: Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 68462   Accepted: 28369 Description Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet conn

最小生成树 prime kruskal

带权图分为有向和无向 无向图的最短路径又叫做最小生成树,有prime算法和kruskal算法: 有向图的最短路径算法,有dijkstra算法和floyd算法. 生成树的概念:联通图G的一个子图如果是一棵包含G的所有顶点的树,则该子图称为G的生成树 生成树是联通图的极小连通子图.所谓极小是指:若在树中任意增加一条边,则 将出现一个回路:若去掉一条边,将会使之编程非连通图.生成树各边的权 值总和称为生成素的权.权最小的生成树称为最小生成树,常用的算法有prime算法和kruskal算法. 最小生成树

最小生成树 prime poj1287

poj1287 裸最小生成树 AC代码 1 #include "map" 2 #include "queue" 3 #include "math.h" 4 #include "stdio.h" 5 #include "string.h" 6 #include "iostream" 7 #include "algorithm" 8 #define abs(x) x &g

hdu1875(最小生成树prime)

思路:一开始想用贪心来着,发现贪心有缺陷,然后就用了最小生成树来写,这里用了prime算法,首先,先建个图,两点之间的边的权值就是两个点的距离,然后直接prime模板 代码 #include<iostream> #include<algorithm> #include<cstring> #include<math.h> #include<cstdio> const int inf=0x7fffffff; using namespace std;

POJ - 2485(最小生成树.prime)

题目链接: http://poj.org/problem?id=2485 题目: Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 36525   Accepted: 16329 Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the

poj 2031 Building a Space Station【最小生成树prime】【模板题】

Building a Space Station Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5699   Accepted: 2855 Description You are a member of the space station engineering team, and are assigned a task in the construction process of the station. You ar