【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 ...
随机推荐
- wins和linux 系统不同编码格式导致的.py执行问题: bad interpreter: No such or file directory
我在win7上用IDLE编写了一个python文件(MyTopo.py),但是用putty传到VM中的ubuntu系统中,用 ./MyTopo方式执行. 显示: /bin/sh^M: bad inte ...
- Amazon EC2上搭建VPN服务器
Amazon EC2 提供了一年免费试用,Micro Instance,配置是 1G 内存,共享 CPU,和每月 15G 的流量.搭一个 VPN 服务器绰绰有余了.操作系统我选的是 Amazon Li ...
- Java并发——线程池Executor框架
线程池 无限制的创建线程 若采用"为每个任务分配一个线程"的方式会存在一些缺陷,尤其是当需要创建大量线程时: 线程生命周期的开销非常高 资源消耗 稳定性 引入线程池 任务是一组逻辑 ...
- jquery click事件的可选参数data的作用
$("#ID").click({x:"value"},function (e) { alert(e.data.x); });
- SQL Server 2008 Values 新用途
SQL Server 2008中新增功能:可以使用单个Insert命令插入多行. Create table Demo_Values (PKID int not null identity(1,1) p ...
- 解决Win10 SVN图标不显示问题
进入注册表HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\ShellIconOverlayIdentifie ...
- dbms_job涉及到的知识点
用于安排和管理作业队列,通过使用作业,可以使ORACLE数据库定期执行特定的任务. 一.dbms_job涉及到的知识点1.创建job:variable jobno number;dbms_job.su ...
- @class的基本使用
2-@class 的基本使用 1, @class的作用 @class 允许简单的引用类,即类的声明.告诉编译器,后面代码中可能会使用到的类名. 好比函数声明一样. 2, #import的作用 与 #i ...
- Hack--兼容性测试
CSS hack由于不同的浏览器,比如Internet Explorer 6,Internet Explorer 7,Mozilla Firefox等,对CSS的解析认识不一样,因此会导致生成的页面效 ...
- ubuntu thinkphp pathinfo 404等问题
这个问题 困扰了我一天,由于对nginx的配置文件中的各种变量不懂.配置起来很麻烦,从网上搜索的,感觉合适自己的不多!!! 找啊找啊..终于找一篇!!!! 我的环境: php ubuntu 12.04 ...