T4 最小差异值 dvalue

T4 最小差异值 dvalue

【问题描述】

  P 省刚经历一场不小的地震,所有城市之间的道路都损坏掉了,所以省长想请你将城市之间的道路重修一遍。
  因为很多城市之间的地基都被地震破坏导致不能修公路了,所以省长给定了你一些城市对,在这些城市对之间可以修公路,并且都有相应的价格。而且因为施工队伍有限,所以省长要求用尽量少的道路将所有的城市连通起来,这样施工量就可以尽量少,道路可视为无向边,且数据保证至少有一种连通的方案。不过,省长为了表示自己的公正无私,要求在满足上述条件的情况下,选择一种方案,使得该方案中最贵道路的价格和最便宜道路的价格的差值尽量小,即使这样的方案会使总价提升很多也没关系。
  那么,请你尽快地安排一种合理的方案,满足省长的要求。

【输入格式】

  第一行两个数 N,M,表示城市的个数以及可以修的公路数;
  第二行开始 M 行,每行三个数 a,b,c,表示 a,b 之间可以修一条价值 c 的无向
道路。

【输出格式】

  一个数表示该方案中最大边减去最小边的值,要求要尽量的小。

【样例输入】

5 10
1 2 9384
1 3 887
1 4 2778
1 5 6916
2 3 7794
2 4 8336
2 5 5387
3 4 493
3 5 6650
4 5 1422

【样例输出】

1686

【样例说明】
选第 4,5,6,9 条边即可。
【数据说明】
30%数据满足 N<=M<=20
100%数据满足 N<=M<=5000,0<c<=50000;

Solution

最小生成树,最小瓶颈生成树

①m^2

将边排序,枚举一个下界(上界),做最小生成树(不连通即可退出),

②mlogm

二分一个

Code

// <dvalue.cpp> - Thu Oct  6 08:17:54 2016
// This file is made by YJinpeng,created by XuYike‘s black technology automatically.
// Copyright (C) 2016 ChangJun High School, Inc.
// I don‘t know what this program is.

#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#define MOD 1000000007
#define INF 1e9
#define IN inline
#define RG register
using namespace std;
typedef long long LL;
typedef long double LB;
const int MAXN=5010;
const int MAXM=100010;
inline int max(int &x,int &y) {return x>y?x:y;}
inline int min(int &x,int &y) {return x<y?x:y;}
inline int gi() {
	register int w=0,q=0;register char ch=getchar();
	while((ch<‘0‘||ch>‘9‘)&&ch!=‘-‘)ch=getchar();
	if(ch==‘-‘)q=1,ch=getchar();
	while(ch>=‘0‘&&ch<=‘9‘)w=w*10+ch-‘0‘,ch=getchar();
	return q?-w:w;
}
struct edge{
    int u,v,w;
    bool operator<(edge a)const{return w<a.w;}
}e[MAXM];
int f[MAXN],ans;
IN int find(int x){return x==f[x]?x:f[x]=find(f[x]);}
void pri(){printf("%d",ans);exit(0);}
int main()
{
	freopen("dvalue.in","r",stdin);
	freopen("dvalue.out","w",stdout);
    int n=gi(),m=gi(),M=0;ans=INF;
    for(int i=1;i<=m;i++)e[i]=(edge){gi(),gi(),gi()};
    sort(e+1,e+1+m);
    for(int i=1;i<=m;i++){
        for(int j=1;j<=n;j++)f[j]=j;
        for(int j=i;j<=m;j++)
            if(find(e[j].u)!=find(e[j].v)){
                M=e[j].w;f[f[e[j].u]]=f[e[j].v];
            }
        find(1);int x=f[1];
        for(int j=2;j<=n;j++){
            find(j);if(f[j]!=x)pri();
        }
        ans=min(ans,M-e[i].w);
    }pri();
	return 0;
}

  

时间: 2024-08-08 17:52:31

T4 最小差异值 dvalue的相关文章

c编程:求出4&amp;#215;4矩阵中最大和最小元素值及其所在行下标和列下标,求出两条主对角线元素之和。

//求出4×4矩阵中最大和最小元素值及其所在行下标和列下标,求出两条主对角线元素之和 #include <stdio.h> int main() { int sum=0; int max,min; int max1,max2;//记录最大值的坐标 int min1,min2;//记录最小值的坐标 int i,j; int a[4][4]; //为数组赋值 for(i=0;i<4;i++) { for(j=0;j<4;j++) { scanf("%d",&

POJ 2253 Frogger(Dijkstra变形——最短路径最小权值)

题目链接: http://poj.org/problem?id=2253 Description Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Frog who is sitting on another stone. He plans to visit her, but since the water is dirty and full of tourists' suns

HDU 4738——Caocao&#39;s Bridges——————【求割边/桥的最小权值】

Caocao's Bridges Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4738 Description Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi. But he wouldn't give up. Caocao's army st

POJ 3686 The Windy&#39;s 最小权值匹配

点击打开链接 The Windy's Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3788   Accepted: 1630 Description The Windy's is a world famous toy factory that owns M top-class workshop to make toys. This year the manager receives N orders for toys.

求满足从1加到m的和大于1000的最小m值

求满足从1加到m的和大于1000的最小m值. 解: >> mysum=0; >> for m=1:1000 mysum=mysum+m; if(mysum>1000)break;end end >> m m = 45

HDU 3488--Tour【最小费用最大流 &amp;&amp; 有向环最小权值覆盖 &amp;&amp; 经典】

Tour Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 2308    Accepted Submission(s): 1156 Problem Description In the kingdom of Henryy, there are N (2 <= N <= 200) cities, with M (M <= 3000

c编程:求出4&#215;4矩阵中最大和最小元素值及其所在行下标和列下标,求出两条主对角线元素之和。

//求出4×4矩阵中最大和最小元素值及其所在行下标和列下标,求出两条主对角线元素之和 #include <stdio.h> int main() { int sum=0; int max,min; int max1,max2;//记录最大值的坐标 int min1,min2;//记录最小值的坐标 int i,j; int a[4][4]; //为数组赋值 for(i=0;i<4;i++) { for(j=0;j<4;j++) { scanf("%d",&

笔试算法题(55):快速排序实现之非递归实现,最小k值选择(non-recursive version, Minimal Kth Selection of Quick Sort)

议题:快速排序实现之五(非递归实现,短序列优先处理,减少递归栈大小) 分析: 算法原理:此算法实现适用于系统栈空间不足够快速排序递归调用的需求,从而使用非递归实现快速排序算法:使用显示下推栈存储快速排序中的每一次划分结果 (将left和right都压入堆栈),并且首先处理划分序列较短的子序列(也就是在得到一次划分的左右部分时,首先将长序列入栈,然后让段序列入栈), 这样可以保证当快速排序退化的线性效率的时候,栈大小仍旧在㏒N范围内.算法策略类似于最小子树优先遍历规则: 弱势:当序列已经就绪,每次

hdu 4738 Caocao&#39;s Bridges(桥的最小权值+去重)

http://acm.hdu.edu.cn/showproblem.php?pid=4738 题目大意:曹操有一些岛屿被桥连接,每座都有士兵把守,周瑜想把这些岛屿分成两部分,但他只能炸毁一条桥,问最少需要派几个士兵去;如果不能完成输出-1 1:如果这些岛屿不连通,则不需要派人前去 2:如果桥的守卫是0的话也得派一人去炸毁 3:如果不能完成输出-1 4:输出最少需派的人数 #include<stdio.h> #include<string.h> #include<math.h&