Kruscal 、 Prime Template

Kruscal  Template :

很裸的Kruscal Template(求最小生成树中最长路,即最短路中最长路)

//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
#include <stdio.h>
#include <iostream>
#include <climits>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std;

const int INF = 0x3f3f3f3f;
const int MAX = 10500;

int root[MAX],n,m,cnt;
struct Edge{
    int s,e;
    int value;
}edge[MAX];

bool cmp(Edge a, Edge b){
    return a.value < b.value;
}

void init(){
    for(int i = 1; i <= n; ++i)
        root[i] = i;
}

int find(int x){
    return root[x] == x ? x : (root[x] = find(root[x]));
}

void merge(int a,int b){
    if(a < b)
        root[b] = a;
    else
        root[a] = b;
}

void kruskal(){
    int i,j;
    cnt = 0;
    for(i = 1; i <= m; ++i){
        int a = find(edge[i].s);
        int b = find(edge[i].e);
        if(a != b){
            merge(a,b);
            ++cnt;
        }
        if(cnt >= n-1){
            printf("%d\n",edge[i].value);
            break;
        }
    }
}
int main(){
   int i,j;
   while(scanf("%d%d",&n,&m) != EOF){
       for(i = 1; i <= m; ++i)
           scanf("%d%d%d",&edge[i].s,&edge[i].e,&edge[i].value);
       sort(edge + 1, edge + 1 + m, cmp);
       init();
       kruskal();
   }
   return 0;
}


Prime Template :

备注: map 初始化为 INF 无穷大

double prim(){
    bool vis[MAXN];
    memset(vis, 0, sizeof(vis));
    double dis[MAXN];
    double ans = 0;
    int i,j;
    vis[1] = true;
    for(i = 1; i <= n; ++i)
        dis[i] = map[1][i];
    for(i = 1; i < n; ++i){
        int temp = INF, flag;
        for(j = 1; j <= n; ++j){
            if(!vis[j] && dis[j] <= temp){
                temp = dis[j];
                flag = j;
            }
        }
        vis[flag] = true;
        ans += dis[flag];
        for(j = 1; j <= n; ++j){
            if(!vis[j] && map[flag][j] < dis[j])
                dis[j] = map[flag][j];
        }
    }
    return ans;
}


Kruscal 、 Prime Template

时间: 2024-08-08 15:09:21

Kruscal 、 Prime Template的相关文章

Miller_Rabin、 Pollard_rho Template

Multiply and pow Function: //计算 (a*b)%c. a,b都是ll的数,直接相乘可能溢出的 // a,b,c <2^63 ll mult_modq(ll a,ll b,ll c){ a %= c; b %= c; ll ret = 0; while(b){ if(b & 1){ret += a;ret %= c;} a <<= 1; if(a >= c)a %= c; b >>= 1; } return ret; } //计算 x^

Centos 7 Docker、docker-compose、Registrator、Consul、Consul Template和Nginx实现高可扩展的Web框架

安装所需软件 Docker Docker-compose 配置docker-compose.yml文件内容如下: #load balancer will automatically update the config using consul-template lb: image: yeasy/nginx-consul-template:latest hostname: lb volumes: - /usr/soft/consul/logapi.conf:/etc/consul-template

AngularJS之延迟加载html template

当使用AngularJs中的routes/views模式建立大型网站或者应用的时候,把所有的自定义文件,如controllers和template等在初始化时全部加载进来,不是一个好的办法.最好的方式是,初始化时仅仅加载所需要的文件.这些文件可能会依赖一个连接或者多个文件,然而它们仅仅被特定的route所使用.当我们切换route时,未被加载的文件将会按需加载.这不但能提高初始化页面的速度,而且可以防止带宽浪费. 网上大部分文章都在讲通过$routeProvider以及第三方服务对control

WPF Template模版之DataTemplate与ControlTemplate的关系和应用【二】

1. DataTemplate和ControlTemplate的关系 学习过DataTemplate和ControlTemplate,你应该已经体会到,控件只是数据的行为和载体,是个抽象的概念,至于它本身长成什么样子(控件内部结构),它的数据会长成什么样子(数据显示结构)都是靠Template生成的.决定控件外观的是ControlTemplate,决定数据外观的是DataTemplate,它们正是Control类的Template和ContentTemplate两个属性值 凡是Template,

angularjs学习笔记--$http、数据渲染到表格、路由、依赖注入、provider

1—$http 可以从服务器中获取更大的数据集,这里使用angularjs的内置服务称为$http.使用angularjs的依赖注入为phoneList组件的控制器提供服务. 在我们的控制器中使用angularjs的$http服务,向我们的web服务器发出http请求,以获取文件中的数据. app/phone-list/phone-list.component1.js: angular.module('phoneList').component('phoneList',{ templateUrl

HTML5 template元素

前言 转自http://www.zhangxinxu.com/wordpress/2014/07/hello-html5-template-tag/ 在单页面应用,我们对页面的无刷新有了更高的要求,HTML不再由后端生成,后端只提供一个REST API,返回JSON数据,模版引擎可以大大方便我们渲染一个视图.而不是吃力的使用 jQeury 去拼接一个DOM. 在现在比较常见的 JS MVC Framework : backbone, emberjs, angularjs 中,模板是非常重要的一个

HTML5 &lt;template&gt;标签元素简介

一.HTML5 template元素初面 <template>元素,基本上可以确定是2013年才出现的.干嘛用的呢,顾名思意,就是用来声明是"模板元素". 目前,我们在HTML中嵌入模板HTML,往往是类似这样的写法: <script type="text/template"> // ... </script> 实际上,并不存在type="text/template"这样的标准写法,<template&g

STL学习笔记--2、空间配置器 allocator

2.1标准接口 allocator::value_type allocator::pointer allocator::const_pointer allocator::reference allocator::const_reference allocator::size_type allocator::difference_type allocator::rebind allocator::allocator()//默认构造函数 allocator::allocator(const allo

前端MVC Vue2学习总结(二)——Vue的实例、生命周期与Vue脚手架(vue-cli)

一.Vue的实例 1.1.创建一个 Vue 的实例 每个 Vue 应用都是通过 Vue 函数创建一个新的 Vue 实例开始的: var vm = new Vue({ // 选项 }) 虽然没有完全遵循 MVVM 模型,Vue 的设计无疑受到了它的启发.因此在文档中经常会使用 vm (ViewModel 的简称) 这个变量名表示 Vue 实例. 1.vue.js就是一个构造器,通过构造器Vue来实例化一个对象:例如:var vm = new Vue({});2.实例化Vue时,需要传入一个参数(选