题目链接: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. Java基础--方法的定义

    1.为什么要有方法? 方法(又叫函数)就是一段特定功能的代码块.方法提高程序的复用性和可读性. 比如,有了方法,我们可以把要重复使用的一段代码提炼出来,然后在每个需要执行这段代码的地方去调用即可. 2 ...

  2. vue环境搭建过程中,npm权限不足问题

    今天在用git bash进行全局安装vue-cli的时候,报错: 必须以管理员权限进行安装才行.所以用cmd命令工具,点击右键命令提示符cmd--------以管理员身份运行--------cd进入到 ...

  3. 【字节校招】【实习】【内推】字节跳动春招(校招或实习均可)以及日常实习内推ing

    本人是年前刚刚入职抖音的应届生,职业认证还未来的级更改,但是这些都不重要.重要的是我们不能错过优秀的你~ 字节跳动的相关福利我就不介绍了,技术实习生是400/天,房补是1500/月,三餐免费,下午茶, ...

  4. 关于adsl vps 拨号ip服务器

    我这几天写了一遍在xp上的文章,但是因为xp上貌似只能使用squid2.6版本的,tinyproxy也不能用,而且怎么弄不出去vps端的端口出来 https://www.cnblogs.com/zen ...

  5. JavaScript中的内存释放

    C.C++语言需要手动管理内存的分配与释放(常用方法:malloc(), calloc(), realloc()和free()等).而JavaScript与Java.C#相似,内置了垃圾回收器,能自动 ...

  6. 【springboot spring mybatis】看我怎么将springboot与spring整合mybatis与druid数据源

    目录 概述 1.mybatis 2.druid 壹:spring整合 2.jdbc.properties 3.mybatis-config.xml 二:java代码 1.mapper 2.servic ...

  7. Netty源码分析一<序一Unix网络I/O模型简介>

    Unix网络 I/O 模型   我们都知道,为了操作系统的安全性考虑,进程是无法直接操作I/O设备的,其必须通过系统调用请求内核来协助完成I/O动作,而内核会为每个I/O设备维护一个buffer.以下 ...

  8. java常用日期类总结

    java 常用的日期类有三个Date.SimpleDateFormat.Calendar

  9. MybatisPlus SQL 打印控制台

    #applicaton.yml 配置 mybatis-plus: configuration: # 是否将sql打印到控制面板(该配置会将sql语句和查询的结果都打印到控制台) log-impl: o ...

  10. FormData/Go分片/分块文件上传

    FormData 接口提供了一种表示表单数据的键值对的构造方式,经过它的数据可以使用 XMLHttpRequest.send() 方法送出,本接口和此方法都相当简单直接.如果送出时的编码类型被设为 & ...