【UVA 10816】 Travel in Desert (最小瓶颈树+最短路)
【题意】
有n个绿洲, m条道路,每条路上有一个温度,和一个路程长度,从绿洲s到绿洲t,求一条道路的最高温度尽量小, 如果有多条, 选一条总路程最短的。
Input
Input consists of several test cases. Your program must process all of them.
The first line contains two integers N and E (1 ≤ N ≤ 100; 1 ≤ E ≤ 10000) where N represents the
number of oasis and E represents the number of paths between them. Next line contains two distinct
integers S and T (1 ≤ S, T ≤ N) representing the starting point and the destination respectively. The
following E lines are the information the group gathered. Each line contains 2 integers X, Y and 2 real
numbers R and D (1 ≤ X, Y ≤ N; 20 ≤ R ≤ 50; 0 < D ≤ 40). It means there is a path between X and
Y , with length D km and highest temperature RoC. Each real number has exactly one digit after the
decimal point. There might be more than one path between a pair of oases.
Output
Print two lines for each test case. The first line should give the route you find, and the second should
contain its length and maximum temperature.
Sample Input
6 9
1 6
1 2 37.1 10.2
2 3 40.5 20.7
3 4 42.8 19.0
3 1 38.3 15.8
4 5 39.7 11.1
6 3 36.0 22.5
5 6 43.9 10.2
2 6 44.2 15.2
4 6 34.2 17.4
Sample Output
1 3 6
38.3 38.3
【分析】
我们可以先求出最大温度的最小值,然后把小于等于这个温度的边加进图中跑最短路。
最短路就不说了,现在就是要求最小瓶颈路。
最小瓶颈路有两个方法,
1、二分+BFS
二分之后沿着小于等于这个温度的边走,只需判断能否走到终点,所以是mlogn的。
2、
但其实可以nlogn把图上所有两点的最小瓶颈路求出来,就是求出最小瓶颈树,那么两点之间的唯一路径就是他们的最小瓶颈路。
而最小生成树就是一个最小瓶颈树。
[其实这个,我也不是很会证明的说- -谁能告诉我- -]
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cmath>
using namespace std;
#define Maxn 110
#define Maxm 10010
#define INF 0xfffffff int n,m,st,ed; struct node
{
int x,y,c,d;
int next;
}tt[Maxm],t[Maxm*];
int len,first[Maxn]; bool cmp(node x,node y) {return x.c<y.c;}
// double mymax(double x,double y) {return x>y?x:y;}
int mymax(int x,int y) {return x>y?x:y;} int fa[Maxn];
int ffa(int x)
{
if(fa[x]!=x) fa[x]=ffa(fa[x]);
return fa[x];
} void ins(int x,int y,int c)
{
t[++len].x=x;t[len].y=y;t[len].c=c;
t[len].next=first[x];first[x]=len;
} int mx[Maxn];
void dfs(int x,int f)
{
for(int i=first[x];i;i=t[i].next) if(t[i].y!=f)
{
int y=t[i].y;
mx[y]=mymax(mx[x],t[i].c);
dfs(y,x);
}
} queue<int > q;
int pre[Maxn],dis[Maxn];
bool inq[Maxn];
void spfa()
{
while(!q.empty()) q.pop();
// for(int i=1;i<=n;i++) dis[i]=INF;
memset(dis,,sizeof(dis));
memset(inq,,sizeof(inq));
q.push(ed);inq[ed]=;dis[ed]=;
while(!q.empty())
{
int x=q.front();
for(int i=first[x];i;i=t[i].next)
{
int y=t[i].y;
if(dis[y]>dis[x]+t[i].c)
{
dis[y]=dis[x]+t[i].c;
pre[y]=x;
if(!inq[y])
{
q.push(y);
inq[y]=;
}
}
}
inq[x]=;q.pop();
}
if(dis[st]>=INF-) return;
int now=st;
while(now!=ed)
{
printf("%d ",now);
now=pre[now];
}
printf("%d\n",ed);
printf("%.1lf %.1lf\n",dis[st]*1.0/,mx[st]*1.0/);
} int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
scanf("%d%d",&st,&ed);
for(int i=;i<=m;i++)
{
double c,d;
scanf("%d%d%lf%lf",&tt[i].x,&tt[i].y,&c,&d);
tt[i].c=(int)round(c*);tt[i].d=(int)round(d*);
}
sort(tt+,tt++m,cmp);
for(int i=;i<=n;i++) fa[i]=i;
int cnt=;
memset(first,,sizeof(first));
len=;
for(int i=;i<=m;i++)
{
if(ffa(tt[i].x)!=ffa(tt[i].y))
{
fa[ffa(tt[i].x)]=ffa(tt[i].y);
cnt++;
ins(tt[i].x,tt[i].y,tt[i].c);
ins(tt[i].y,tt[i].x,tt[i].c);
}
if(cnt==n-) break;
}
mx[ed]=;
dfs(ed,);
len=;
memset(first,,sizeof(first));
for(int i=;i<=m;i++) if(tt[i].c<=mx[st])
{
ins(tt[i].x,tt[i].y,tt[i].d);
ins(tt[i].y,tt[i].x,tt[i].d);
}
spfa();
}
return ;
}
2016-11-01 15:57:34
【UVA 10816】 Travel in Desert (最小瓶颈树+最短路)的更多相关文章
- uva 10816 Travel in Desert(简单的好题~两种方法)
题意: 给出 一个图 点与点之间的路径上有两个权值 路径长度和温度 要求在所走路径中的温度的最大值最小的前提下 走最短路径 解题思路1: 首先用 最小生成树 的方法走出 最小瓶颈路 .把在这期间用到的 ...
- 【洛谷P2504】聪明的猴子 最小瓶颈树
题目大意:给定一张 N 个顶点的完全图,边有边权,求该完全图的一棵最小瓶颈树. 最小瓶颈树:一棵最大边权值在同一张图的所有生成树中最小,即:最大边权值最小的生成树,其值为该树的最大边权的权值. 引理1 ...
- 【UVA10816】Travel in Desert (最小瓶颈路+最短路)
UVA10816 Travel in Desert 题目大意 沙漠中有一些道路,每个道路有一个温度和距离,要求s,t两点间的一条路径,满足温度最大值最小,并且长度最短 输入格式 输入包含多组数据. 每 ...
- 【UVA 11354】 Bond (最小瓶颈生成树、树上倍增)
[题意] n个点m条边的图 q次询问 找到一条从s到t的一条边 使所有边的最大危险系数最小 InputThere will be at most 5 cases in the input file.T ...
- POJ 2253 Frogger【最短路变形/最小生成树的最大权/最小瓶颈树/A到B多条路径中的最小的最长边】
Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Frog who is sit ...
- POJ 2395 Out of Hay 草荒 (MST,Kruscal,最小瓶颈树)
题意:Bessie要从牧场1到达各大牧场去,他从不关心他要走多远,他只关心他的水袋够不够水,他可以在任意牧场补给水,问他走完各大牧场,最多的一次需要多少带多少单位的水? 思路:其实就是要让所带的水尽量 ...
- UVa 11354 邦德(最小瓶颈路+LCA)
https://vjudge.net/problem/UVA-11354 题意: 有n个城市m条道路,每条道路有一个危险系数.先在有若干个询问,要求找到一条从s到t的路,使得途径所有边的最大危险系数最 ...
- POJ 1797 Heavy Transportation 【最大生成树的最小边/最小瓶颈树】
Background Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand bus ...
- 【uva 534】Frogger(图论--最小瓶颈路 模版题)
题意:平面上有N个石头,给出坐标.一只青蛙从1号石头跳到2号石头,使路径上的最长便最短.输出这个值.(2≤N≤200) 解法:最小瓶颈树.而由于这题N比较小便可以用2种方法:1.最短路径中提到过的Fl ...
随机推荐
- Java中的编码问题
下面将侧重介绍java乱码是如何产生的.存在哪些乱码的情况.该如何从根本上解决乱码问题.各位随博主一起征服令人厌烦的java乱码问题吧!!! 一.Java编码转换过程 我们总是用一个java类文件和用 ...
- 基于Windows的套接字相关函数及示例
链接ws2_32.lib库 头文件#include <winsock2.h> int WSAStartup(WORD wVersionRequested,LPWSADATA lpWSADa ...
- CSS3 动画效果合集
@charset "UTF-8"; /*! * animate.css -http://daneden.me/animate * Version - 3.5.1 * License ...
- js表格的输出
<html> <head> <title>隔行变色</title> <script type="text/javascript" ...
- Log4Net详细配置
关于Log4Net配置主要分几步 第一步:下载log4net.dll(log4net官网:http://logging.apache.org/log4net/download_log4net.cgi) ...
- Android 设计随便说说之简单实践(消息流动)
在上面两篇分别说明了设计中较为简单也是很关键的实践点. 第一模块划分,它是根据每个模块所承载的业务,进行划分,是应用程序一个静态的描述. 第二合理组合,它是是将每个模块调动起来,共同实现业务,是一个准 ...
- MVC小系列(八)【改变Areas的FindView顺序】
MVC小系列(八)[改变Areas的FindView顺序] 一般项目比较大的话,会根据模块建立Areas,这样结构清晰,也有利于路由的部署, 1 Areas下有自己的_LayOut模板,而如果希望所有 ...
- Ubuntu 使用top/free查看内存占用大的原因
Ubuntu 使用top/free查看内存占用大的原因 linux/ubuntu下free/top查看内存占用大的原因 使用free/top查看内存占用的时候,吓了一大跳,机器4GB的内存,显 ...
- IOS 高级开发 runtime(二)
二.移魂大法 使用runtime还可以交换两个函数.先贴上代码和执行结果. #import <Foundation/Foundation.h> @interface DZLPerson : ...
- VBA 打印设置相关属性及方法
打印设置说明,以下均为默认值. With ActiveSheet.PageSetup .PrintTitleRows = "" '工作表打印标题:顶端标题行(R) .PrintTi ...