POJ-2485 Highways + POJ-1258 Agri-Net 【Kruskal】

整理板子的时候翻出来的题,放在一起写是因为都是Kruskal纯板子题。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 #include<cmath>
 6 #include<string>
 7 #include<stack>
 8 #include<queue>
 9 #include<vector>
10 #include<cstdlib>
11 //#include<windows.h>
12 #define first fi
13 #define second se
14 using namespace std;
15 typedef long long ll;
16 const int N = 1e6+10;
17 const int INF = 0x3f3f3f3f;
18 const int inf = - INF;
19 const int mod = 1e9+7;
20 const double pi = acos(-1.0);
21 int n,m;
22 int father[505];
23 void init(){
24     m=0;
25     memset(father,-1,sizeof(father));
26 }
27 struct node {
28     int u,v,w;
29     node(){}
30     node(int u,int v, int w):u(u),v(v),w(w){}
31     bool operator < (const node &rhs)const{
32         return w < rhs.w;
33     }
34 }edge[N];
35 int Find(int x){
36     return father[x]==-1?x:father[x]=Find(father[x]);
37 }
38 int Kruskal(){
39     int maxn=-1;
40     int cnt=0;
41     for(int i=0;i<m;i++){
42         int u=edge[i].u;
43         int v=edge[i].v;
44         if(Find(u)!=Find(v)){
45             father[Find(u)]=Find(v);
46             maxn=max(maxn,edge[i].w);
47             if(++cnt>=n-1) return maxn;
48         }
49     }
50     return -1;
51 }
52 int main(){
53     int T;
54     scanf("%d",&T);
55     while(T--){
56         scanf("%d",&n);
57         init();
58         int t;
59         for(int i=1;i<=n;i++){
60             for(int j=1;j<=n;j++){
61                 scanf("%d",&t);
62                 edge[m++]=node(i,j,t);
63             }
64         }
65         sort(edge,edge+m);
66         printf("%d\n",Kruskal());
67     }
68     //system("pause");
69     return 0;
70 }

1258可以直接用2485改改就交,注意要多组输入。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<stack>
#include<queue>
#include<vector>
#include<cstdlib>
#include<windows.h>
#define first fi
#define second se
using namespace std;
typedef long long ll;
const int N = 1e6+10;
const int INF = 0x3f3f3f3f;
const int inf = - INF;
const int mod = 1e9+7;
const double pi = acos(-1.0);
ll n,m;
ll father[505];
void init(){
    m=0;
    memset(father,-1,sizeof(father));
}
struct node {
    ll u,v,w;
    node(){}
    node(ll u,ll v, ll w):u(u),v(v),w(w){}
    bool operator < (const node &rhs)const{
        return w < rhs.w;
    }
}edge[N];
ll Find(int x){
    return father[x]==-1?x:father[x]=Find(father[x]);
}
ll Kruskal(){
    ll sum=0;
    ll cnt=0;
    for(ll i=0;i<m;i++){
        ll u=edge[i].u;
        ll v=edge[i].v;
        if(Find(u)!=Find(v)){
            father[Find(u)]=Find(v);
            sum+=edge[i].w;
            if(++cnt>=n-1) return sum;
        }
    }
    return -1;
}
int main(){
    //int T;
    //scanf("%d",&T);
    while(~scanf("%lld",&n)){//注意多组输入
        init();
        ll t;
        for(ll i=1;i<=n;i++){
            for(ll j=1;j<=n;j++){
                scanf("%lld",&t);
                edge[m++]=node(i,j,t);
            }
        }
        sort(edge,edge+m);
        printf("%d\n",Kruskal());
    }
    system("pause");
    return 0;
}

原文地址:https://www.cnblogs.com/yoshinaripb/p/11786041.html

时间: 2024-08-08 01:25:16

POJ-2485 Highways + POJ-1258 Agri-Net 【Kruskal】的相关文章

poj 2485 Highways(最小生成树)

题目链接:http://poj.org/problem?id=2485 Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian government is aware of this problem. They're plann

poj 2485 Highways

链接:poj 2485 题意:输入n个城镇相互之间的距离,输出将n个城镇连通费用最小的方案中修的最长的路的长度 这个也是最小生成树的题,只不过要求的不是最小价值,而是最小生成树中的最大权值,只需要加个判断 比较最小生成树每条边的大小就行 #include<cstdio> #include<algorithm> using namespace std; int f[510],n,m; struct stu { int a,b,c; }t[20100]; int cmp(struct

POJ 2485 Highways &amp;&amp; HDU1102(20/200)

题目链接:Highways 没看题,看了输入输出,就有种似曾相识的感觉,果然和HDU1102 题相似度99%,但是也遇到一坑 cin输入竟然TLE,cin的缓存不至于这么狠吧,题目很水,矩阵已经告诉你了,就敲个模板就是了,5分钟,1A 题意就是打印,最小生成树的最大边权,改了改输入,水过 这个题完了,我的个人POJ计划进度以完成 20/200,这其中主要是图论的题,等下周把POJ计划图论的题目打完,就回头打模拟!我就不信还能比图论难 #include <iostream> #include &

POJ 1041 John&#39;s trip 无向图的【欧拉回路】路径输出

欧拉回路第一题TVT 本题的一个小技巧在于: [建立一个存放点与边关系的邻接矩阵] 1.先判断是否存在欧拉路径 无向图: 欧拉回路:连通 + 所有定点的度为偶数 欧拉路径:连通 + 除源点和终点外都为偶数 有向图: 欧拉回路:连通 + 所有点的入度 == 出度 欧拉路径:连通 + 源点 出度-入度=1 && 终点 入度 - 出度 = 1 && 其余点 入度 == 出度: 2.求欧拉路径 : step 1:选取起点(如果是点的度数全为偶数任意点为S如果有两个点的度数位奇数取一

POJ 3683 Priest John&#39;s Busiest Day 【2-Sat】

这是一道裸的2-Sat,只要考虑矛盾条件的判断就好了. 矛盾判断: 对于婚礼现场 x 和 y,x 的第一段可以和 y 的第一段或者第二段矛盾,同理,x 的第二段可以和 y 的第一段或者第二段矛盾,条件是 x 的 1或2 段与 y 的 1或2 段有重合,那么选了 x 某一段就不能选择其矛盾的那一段,那就只能选择 y 中的另一段,建立一条 x (u)-> y ( v的对立 ),同理,x (u的对立)<- y ( v ) . 真的,2-sat千万不要用邻接链表!!!卡死卡死卡死!!!稠密图!!!不要

hdoj 1863 畅通工程 【最小生成树】+【kruskal】

题意:... 难点:如何判断是不是信息不全:在输入的时候建立并查集,之后判断有几个节点就可以了,剩下的就是kruskal算法. 代码: #include<stdio.h> #include<string.h> #include<algorithm> #define MAXN 105 #define INF 0x3f3f3f3f using std::sort; struct node{ int from; int to; int w; }edges[MAXN*MAXN]

poj 1258 Agri-Net poj 2485 Highways

http://poj.org/problem?id=1258 多么单纯的mst! #include<iostream> #include<string.h> using namespace std; const int INF=103; int graph[INF][INF]; int f1[INF]; int low[INF]; int res,n; int prim() { f1[1]=1; memset(low,0,sizeof(low)); for(int i=1; i&l

POJ 2485 Highways (kruskal 最小生成树)

Highways POJ 2485so that it will be possible to drive between any pair of towns without leaving the highway system. Flatopian towns are numbered from 1 to N. Each highway connects exactly two towns. All highways follow straight lines. All highways ca

poj 2485 Highways (最小生成树)

链接:poj 2485 题意:输入n个城镇相互之间的距离,输出将n个城镇连通费用最小的方案中修的最长的路的长度 这个也是最小生成树的题,仅仅只是要求的不是最小价值,而是最小生成树中的最大权值.仅仅须要加个推断 比較最小生成树每条边的大小即可 kruskal算法 #include<cstdio> #include<algorithm> using namespace std; int f[510],n,m; struct stu { int a,b,c; }t[20100]; int

POJ 2485 Highways【最小生成树最大边,Prime算法】

Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28592   Accepted: 13037 Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Fl