POJ 1797 【一种叫做最大生成树的很有趣的贪心】【也可以用dij的变形思想~】
题意:
给一个无向图,找1到n所有的路中每条路最小权值的最大值!
屌丝一开始的思想是利用dij的变形~
但是==屌丝忘记了更新dis数组~结果TLE无数次...
说正经的~dij的变形思想是这样的if(dis[now]<min(dis[pos],w[pos][now]))则更新dis[now]....这种变形的证明和原来dij的证明是一样的~大家自己脑补啊~
heap优化的dij用时250ms~
上代码~
恶心的是不知道邻接表要搞多少边~这里RE无数次==
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int min(int a,int b)
{
if(a<b)
return a;
return b;
}
struct st
{
int w,id;
st(int a,int b){w=a;id=b;}
st(){}
};
struct cmp
{
bool operator()(const st &a,const st &b)
{
return a.w<b.w;
}
};
int n,m;
const int inf=;
int dis[];
struct edge
{
int id,w;
edge *next;
};
edge *adj[];
edge edges[];
int ednum;
inline void addEdge(int a,int b,int c)
{
edge *aa;
aa=&edges[ednum];
ednum++;
aa->id=b;
aa->w=c;
aa->next=adj[a];
adj[a]=aa;
}
int solve()
{
for(int i=; i<=n; i++)
{
dis[i]=-;
}
dis[]=inf;
st tmp;
priority_queue<st,vector<st>,cmp>q;
q.push(st(inf,));
while(!q.empty())
{
tmp=q.top();
q.pop();
if(tmp.id==n)
return tmp.w;
for(edge *p=adj[tmp.id];p;p=p->next)
{
if(dis[p->id]<min(dis[tmp.id],p->w))
{
dis[p->id]=min(dis[tmp.id],p->w);
q.push(st(dis[p->id],p->id));
}
}
}
}
int main()
{
int t,a,b,c;
scanf("%d",&t);
for(int tt=; tt<=t; tt++)
{
scanf("%d%d",&n,&m);
ednum=;
for(int i=; i<=n; i++)
adj[i]=NULL;
for(int i=; i<=m; i++)
{
scanf("%d%d%d",&a,&b,&c);
addEdge(a,b,c);
addEdge(b,a,c);
}
printf("Scenario #%d:\n%d\n",tt,solve());
if(tt!=t)
printf("\n");
}
}
接下来是最大生成树的思想~
采用把边从大到小排序的思想,然后用并查集实现的方法~
每次加入边之后确认1和n的连通情况~当第一次实现两者连通之后就输出那条边的权值~
证明也是贪心的思想~大家自己脑补~
时间稍慢 344ms
上代码~
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int inf=;
int n,m;
bool ok;
int rel;
int min(int a,int b)
{
if(a<b)
return a;
return b;
}
struct edge
{
int st,ed,w;
};
int me[];
edge edges[];
bool cmp(edge a,edge b)
{
return a.w>b.w;
}
int findme(int a)
{
if(a!=me[a])
return me[a]=findme(me[a]);
return a;
} int main()
{
int t;
scanf("%d",&t);
for(int tt=;tt<=t;tt++)
{
ok=;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
{
me[i]=i;
}
for(int i=;i<m;i++)
{
scanf("%d%d%d",&edges[i].st,&edges[i].ed,&edges[i].w);
}
sort(edges,edges+m,cmp);
for(int i=;i<m;i++)
{
int tmpa=findme(edges[i].st);
int tmpb=findme(edges[i].ed);
if(tmpa!=tmpb)
{
me[tmpb]=tmpa;
}
tmpa=findme();
tmpb=findme(n);
if(tmpa==tmpb)
{
rel=edges[i].w;
break;
}
}
printf("Scenario #%d:\n%d\n",tt,rel);
if(tt!=t)
printf("\n");
}
return ;
}
POJ 1797 【一种叫做最大生成树的很有趣的贪心】【也可以用dij的变形思想~】的更多相关文章
- poj 1797 Heavy Transportation(最大生成树)
poj 1797 Heavy Transportation Description Background Hugo Heavy is happy. After the breakdown of the ...
- (POJ 1797) Heavy Transportation 最大生成树
题目链接:http://poj.org/problem?id=1797 Description Background Hugo Heavy is happy. After the breakdown ...
- POJ 1797 Heavy Transportation(最大生成树/最短路变形)
传送门 Heavy Transportation Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 31882 Accept ...
- POJ 1797 Heavy Transportation 【最大生成树的最小边/最小瓶颈树】
Background Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand bus ...
- POJ 1797 Heavy Transportation (最大生成树)
题目链接:POJ 1797 Description Background Hugo Heavy is happy. After the breakdown of the Cargolifter pro ...
- POJ 1797 Heavy Transportation (Dijkstra)
题目链接:POJ 1797 Description Background Hugo Heavy is happy. After the breakdown of the Cargolifter pro ...
- POJ 1797 Heavy Transportation (Dijkstra变形)
F - Heavy Transportation Time Limit:3000MS Memory Limit:30000KB 64bit IO Format:%I64d & ...
- POJ 1797 Heavy Transportation / SCU 1819 Heavy Transportation (图论,最短路径)
POJ 1797 Heavy Transportation / SCU 1819 Heavy Transportation (图论,最短路径) Description Background Hugo ...
- POJ.1797 Heavy Transportation (Dijkstra变形)
POJ.1797 Heavy Transportation (Dijkstra变形) 题意分析 给出n个点,m条边的城市网络,其中 x y d 代表由x到y(或由y到x)的公路所能承受的最大重量为d, ...
随机推荐
- HTTP 方法:GET 对比 POST 转自w3school
两种最常用的 HTTP 方法是:GET 和 POST. 什么是 HTTP? 超文本传输协议(HTTP)的设计目的是保证客户机与服务器之间的通信. HTTP 的工作方式是客户机与服务器之间的请求-应答协 ...
- 语音行业技术领先者Nuance诚招ASR/NLP研发工程师和软件工程师
Nuance is a leading provider of voice and language solutions for businesses and consumers around the ...
- springboot设置接口超时
springboot 设置接口超时 1.配置文件 application.properties中加了,意思是设置超时时间为20000ms即20s, spring.mvc.async.request-t ...
- 事件捕获 & 事件冒泡
<body> <div id="div1"> <div id="div2"> <div id="div3&q ...
- Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown error 1146
问题介绍: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown error 1146 MySql语法错误, 但是错 ...
- Vickers Vane Pump - Hydraulic Vane Pump Failure: Cavitation, Mechanical Damage
One of our readers recently wrote to me about the following questions: “Recently, we purchased a sec ...
- fio测试nvme性能
#cat /sys/block/nvme0n1/queue/scheduler none #cat /sys/block/sda/queue/scheduler noop deadline [cfq] ...
- Proguard配置文件内容
-injars elec-bendao-1.2.jar-outjars elec-bendao-1.2-end.jar -libraryjars lib\charsets.jar-libraryjar ...
- Swift中Singleton的实现
一.意图 保证一个类公有一个实例,并提供一个访问它的全局访问点. 二.使用场景 1.使用场景 当类只能有一个实例而且客户可以从一个众所周知的访问点访问它时 当这个唯一实例应该是通过子类化可扩展的,并且 ...
- 洛谷 P3131 子共七
看到这一题第一印象就是暴力好打,$O(n^2)$,预计得分$70$分 这明显满足不了啊,我们要用到前缀和. $sum[i]$记录到i的前缀和,区间$[a,b]$的和就是$sum[b]-sum[a-1] ...