题目链接: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. 2020年春招面试必备Spring系列面试题129道(附答案解析)

    前言 关于Spring的知识总结了个思维导图分享给大家   1.不同版本的 Spring Framework 有哪些主要功能?   2.什么是 Spring Framework? Spring 是一个 ...

  2. sql04

    1.类型转换 ),ClassId)+name from [user]; 2.一次性插入多条数据 3.日期函数 1)getdate() 返回当前日期 2)dateadd 计算增加后的时间 ,'2020- ...

  3. JavaScript常见的六种继承方式

    前言 面向对象编程很重要的一个方面,就是对象的继承.A 对象通过继承 B 对象,就能直接拥有 B 对象的所有属性和方法.这对于代码的复用是非常有用的. 大部分面向对象的编程语言,都是通过"类 ...

  4. proxyTable的配置

    在dev环境下面: proxyTable: { '/api': { target: 'http://api.douban.com/v2', //主域名,以前我都写192.168.2.57:80,这里跨 ...

  5. PHP 深度理解preg_quote()函数

    php手册上说,preg_quote()函数的作用是转义正则表达式字符.那么下面我们来深入了解下这个函数是怎么使用的: 说明:preg_quote()函数常和preg_replace()函数一起使用. ...

  6. Django中的session的使用

    一.Session 的概念 cookie 是在浏览器端保存键值对数据,而 session 是在服务器端保存键值对数据 session 的使用依赖 cookie:在使用 Session 后,会在 Coo ...

  7. python基本数据类型的操作

    1 列表和元组 1.列表基本操作 1. 列表赋值 a = [1,2,3,4,5,6,7,8] a[0] = 100 #the result : [100, 2, 3, 4, 5, 6, 7, 8] 2 ...

  8. sentinel 规则持久化到nacos

    问题描述 Sentinel Dashboard中添加的规则是存储在内存中的,只要项目一重启规则就丢失了 此处将规则持久化到nacos中,在nacos中添加规则,然后同步到dashboard中: 后面研 ...

  9. 20 本地SQL查询

    Spring Data JPA同样也支持sql语句的查询 //nativeQuery : 使用本地sql的方式查询 @Query(value="select * from customer& ...

  10. node打开本地应用程序

    1.打开浏览器 最简单的方法: const cp = require('child_process') cp.exec('start http://127.0.0.1:8889/'); // 自动打开 ...