题目链接:http://poj.org/problem?id=1797

题意:给定n个点,m条边,每条边连接两点切有权值。求点1到点n的路径的上的最小边的值最大。。。

翻别人博客找到的题,方法挺多的,直接跑一个最大生成树就行,或者是一个最短路算法也行

我自己用了prim和kruskal 的方法来做

虽然用最短路也可以轻松过,但是我还是选择了生成树

prim

 #include<cstdio>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<queue>
#define maxn 1005
using namespace std;
struct node{
int u,v,w,nxt;
}e[maxn*maxn]; int n,m,low[maxn],mst[maxn],head[maxn],vis[maxn],ans; int read(){
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
} int tot;
void adde(int u,int v,int w){
e[tot]=(node){u,v,w,head[u]};
head[u]=tot++;
} void first(){
ans=;
memset(low,,sizeof(low));
memset(mst,,sizeof(mst));
memset(head,-,sizeof(head));
memset(vis,,sizeof(vis));tot= ;
} void prim(int num){
int u=,maxx=-ans,maxid;
for(int i=head[u];i!=-;i=e[i].nxt){
int v=e[i].v;
mst[v]=u;low[v]=e[i].w;
}vis[u]=;
int nn=n-;
while(nn--){
for(int i=;i<=n;i++){
if(low[i]&&mst[i]){
if(low[i]>maxx){
maxx=low[i];maxid=i;
}
}
}
ans=min(maxx,ans);maxx=;
low[maxid]=;mst[maxid]=;vis[maxid]=;
if(maxid==n)break;
for(int i=head[maxid];i!=-;i=e[i].nxt){
int v=e[i].v;
if(e[i].w>low[v]&&!vis[v]){
mst[v]=maxid;low[v]=e[i].w;
}
}
}
printf("Scenario #%d:\n%d\n\n",num,ans);
} int main(){
int T;T=read();int h=;
while(T--){
first();h++;
n=read();m=read();
for(int i=;i<=m;i++){
int u,v,w;
u=read();v=read();w=read();
adde(u,v,w);adde(v,u,w);
}
prim(h);
}
}

kruskal

 #include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<queue>
#define maxn 1005
using namespace std; struct node{
int u,v,w,nxt;
}e[maxn*maxn]; int n,m,fa[maxn],sum,ans,vis[maxn]; int tot;
void adde(int u,int v,int w){
e[tot]=(node){u,v,w};
tot++;
} void first(){
ans=;sum=;tot=;
for(int i=;i<=n;i++)fa[i]=i;
} int find(int x){
if(fa[x]==x)return x;
return fa[x]=find(fa[x]);
} int comp(const void*a,const void*b){
return (*(struct node*)a).w<(*(struct node*)b).w?:-;
} int read(){
int xx=,ff=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')ff=-;ch=getchar();}
while(ch>=''&&ch<=''){xx=xx*+ch-'';ch=getchar();}
return xx*ff;
} int can(int x,int y){
int fx=find(x),fy=find(y);
if(fx!=fy)return ;
return ;
} int main(){
int T,k=;T=read();
while(T--){
k++;n=read();m=read();
first();memset(vis,,sizeof(vis));
for(int i=;i<=m;i++){
int u=read(),v=read(),w=read();
adde(u,v,w);adde(v,u,w);
}e[].w=ans;
qsort(e+,tot+,sizeof(e[]),comp);
for(int i=;i<=tot;i++){
int u=e[i].u,v=e[i].v;
int fu=find(u),fv=find(v);
if(fu!=fv){
fa[fv]=fu;ans=min(ans,e[i].w);
}
if(can(,n)){
printf("Scenario #%d:\n%d\n\n",k,ans);break;
}
}
}
}

PS:注意一下输出的格式,容易错的。

还有就是判断算法的结束条件,并不是当所有的点都加入了,而是当1能到n 的时候就可以跳出了

[poj1797]Heavy Transportation<最大生成树prim&kruskal>的更多相关文章

  1. POJ--1797 Heavy Transportation (最短路)

    题目电波: POJ--1797 Heavy Transportation n点m条边, 求1到n最短边最大的路径的最短边长度 改进dijikstra,dist[i]数组保存源点到i点的最短边最大的路径 ...

  2. POJ1797 Heavy Transportation 【Dijkstra】

    Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 21037   Accepted:  ...

  3. (Dijkstra) POJ1797 Heavy Transportation

    Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 53170   Accepted:  ...

  4. POJ1797 Heavy Transportation —— 最短路变形

    题目链接:http://poj.org/problem?id=1797 Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K T ...

  5. [POJ1797] Heavy Transportation(最大生成树 || 最短路变形)

    传送门 1.最大生成树 可以求出最大生成树,其中权值最小的边即为答案. 2.最短路 只需改变spfa里面的松弛操作就可以求出答案. ——代码 #include <queue> #inclu ...

  6. POJ 1797 Heavy Transportation (最大生成树)

    题目链接:POJ 1797 Description Background Hugo Heavy is happy. After the breakdown of the Cargolifter pro ...

  7. (POJ 1797) Heavy Transportation 最大生成树

    题目链接:http://poj.org/problem?id=1797 Description Background Hugo Heavy is happy. After the breakdown ...

  8. POJ1797 Heavy Transportation

    解题思路:典型的Kruskal,不能用floyed(会超时),上代码: #include<cstdio> #include<cstring> #include<algor ...

  9. POJ1797 Heavy Transportation (堆优化的Dijkstra变形)

    Background Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand bus ...

随机推荐

  1. 第一篇:注册中心Eureka

    1.什么是Eureka,有什么用? Eureka是Netflix开源的一款提供服务注册和发现的产品,它提供了完整的Service Registry和Service Discovery实现.也是spri ...

  2. Web网页布局的主要方式

    一.静态布局(static layout) 即传统Web设计,网页上的所有元素的尺寸一律使用px作为单位. 1.布局特点 不管浏览器尺寸具体是多少,网页布局始终按照最初写代码时的布局来显示.常规的pc ...

  3. HTML、CSS笔记

    盒模型 在CSS中,使用标准盒模型描述这些矩形盒子中的每一个.这个模型描述了元素所占空间的内容.每个盒子有四个边:外边距边, 边框边, 内填充边 与 内容边. 在标准模式下,一个块的总宽度= widt ...

  4. CSS3实现一个旋转的花朵

    要效果图如下: 实现原理:其实很简单,就是中间的圆圈定位在中间,其他的6个圆圈,进行不同的绝对定位,然后进行旋转!代码: <!DOCTYPE html> <html lang=&qu ...

  5. JS对象之封装(二)

    JS 对象封装的常用方式 1.常规封装 function Person (name,age){ this.name = name; this.age = age; } Pserson.prototyp ...

  6. 学习使用Guava Cache

    官方文档:https://github.com/google/guava/wiki/CachesExplained 目录 一.guava cache介绍 二.快速入门 2.1.引入依赖 2.2.第一个 ...

  7. Java Opencv 实现 中值滤波器

    原理 Note 以下原理来源于Richard Szeliski 的著作 Computer Vision: Algorithms and Applications 以及 Learning OpenCV ...

  8. 小巧开源的 baresip VOIP 项目

    Baresip is a modular SIP User-Agent with audio and video support https://github.com/alfredh/baresip ...

  9. selenium+options配置文件

    from selenium.webdriver.chrome.options import Options from selenium import webdriver chrome_options ...

  10. iframe 父框架调用子框架的函数

    1.父框架定义: <iframe name="mainframe" id="mainframe" width="100%" scrol ...