【题意】

  有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把图上所有两点的最小瓶颈路求出来,就是求出最小瓶颈树,那么两点之间的唯一路径就是他们的最小瓶颈路。

    而最小生成树就是一个最小瓶颈树。

  [其实这个,我也不是很会证明的说- -谁能告诉我- -]

  1 #include<cstdio>
  2 #include<cstdlib>
  3 #include<cstring>
  4 #include<iostream>
  5 #include<algorithm>
  6 #include<queue>
  7 #include<cmath>
  8 using namespace std;
  9 #define Maxn 110
 10 #define Maxm 10010
 11 #define INF 0xfffffff
 12
 13 int n,m,st,ed;
 14
 15 struct node
 16 {
 17     int x,y,c,d;
 18     int next;
 19 }tt[Maxm],t[Maxm*2];
 20 int len,first[Maxn];
 21
 22 bool cmp(node x,node y) {return x.c<y.c;}
 23 // double mymax(double x,double y) {return x>y?x:y;}
 24 int mymax(int x,int y) {return x>y?x:y;}
 25
 26 int fa[Maxn];
 27 int ffa(int x)
 28 {
 29     if(fa[x]!=x) fa[x]=ffa(fa[x]);
 30     return fa[x];
 31 }
 32
 33 void ins(int x,int y,int c)
 34 {
 35     t[++len].x=x;t[len].y=y;t[len].c=c;
 36     t[len].next=first[x];first[x]=len;
 37 }
 38
 39 int mx[Maxn];
 40 void dfs(int x,int f)
 41 {
 42     for(int i=first[x];i;i=t[i].next) if(t[i].y!=f)
 43     {
 44         int y=t[i].y;
 45         mx[y]=mymax(mx[x],t[i].c);
 46         dfs(y,x);
 47     }
 48 }
 49
 50 queue<int > q;
 51 int pre[Maxn],dis[Maxn];
 52 bool inq[Maxn];
 53 void spfa()
 54 {
 55     while(!q.empty()) q.pop();
 56     // for(int i=1;i<=n;i++) dis[i]=INF;
 57     memset(dis,63,sizeof(dis));
 58     memset(inq,0,sizeof(inq));
 59     q.push(ed);inq[ed]=1;dis[ed]=0;
 60     while(!q.empty())
 61     {
 62         int x=q.front();
 63         for(int i=first[x];i;i=t[i].next)
 64         {
 65             int y=t[i].y;
 66             if(dis[y]>dis[x]+t[i].c)
 67             {
 68                 dis[y]=dis[x]+t[i].c;
 69                 pre[y]=x;
 70                 if(!inq[y])
 71                 {
 72                     q.push(y);
 73                     inq[y]=1;
 74                 }
 75             }
 76         }
 77         inq[x]=0;q.pop();
 78     }
 79     if(dis[st]>=INF-10000) return;
 80     int now=st;
 81     while(now!=ed)
 82     {
 83         printf("%d ",now);
 84         now=pre[now];
 85     }
 86     printf("%d\n",ed);
 87     printf("%.1lf %.1lf\n",dis[st]*1.0/10,mx[st]*1.0/10);
 88 }
 89
 90 int main()
 91 {
 92     while(scanf("%d%d",&n,&m)!=EOF)
 93     {
 94         scanf("%d%d",&st,&ed);
 95         for(int i=1;i<=m;i++)
 96         {
 97             double c,d;
 98             scanf("%d%d%lf%lf",&tt[i].x,&tt[i].y,&c,&d);
 99             tt[i].c=(int)round(c*10);tt[i].d=(int)round(d*10);
100         }
101         sort(tt+1,tt+1+m,cmp);
102         for(int i=1;i<=n;i++) fa[i]=i;
103         int cnt=0;
104         memset(first,0,sizeof(first));
105         len=0;
106         for(int i=1;i<=m;i++)
107         {
108             if(ffa(tt[i].x)!=ffa(tt[i].y))
109             {
110                 fa[ffa(tt[i].x)]=ffa(tt[i].y);
111                 cnt++;
112                 ins(tt[i].x,tt[i].y,tt[i].c);
113                 ins(tt[i].y,tt[i].x,tt[i].c);
114             }
115             if(cnt==n-1) break;
116         }
117         mx[ed]=0;
118         dfs(ed,0);
119         len=0;
120         memset(first,0,sizeof(first));
121         for(int i=1;i<=m;i++) if(tt[i].c<=mx[st])
122         {
123             ins(tt[i].x,tt[i].y,tt[i].d);
124             ins(tt[i].y,tt[i].x,tt[i].d);
125         }
126         spfa();
127     }
128     return 0;
129 }

View Code

2016-11-01 15:57:34

转载于:https://www.cnblogs.com/Konjakmoyu/p/6019717.html

【UVA 10816】 Travel in Desert (最小瓶颈树+最短路)相关推荐

  1. uva 10816 Travel in Desert(简单的好题~两种方法)

    题意: 给出 一个图 点与点之间的路径上有两个权值 路径长度和温度 要求在所走路径中的温度的最大值最小的前提下 走最短路径 解题思路1: 首先用 最小生成树 的方法走出 最小瓶颈路 .把在这期间用到的 ...

  2. 二叉搜索树-创建最小高度树(递归)

    题意: 给定一个有序整数数组,元素各不相同且按升序排列,编写一个算法,创建一棵高度最小的二叉搜索树. 这里回忆一下二叉搜索树的概念: 对于树中的所有子树都有,左子树上的值都小于根节点的值,右子树上的值 ...

  3. 【模板】最小割树(Gomory-Hu Tree)

    传送门 Description 给定一个\(n\)个点\(m\)条边的无向连通图,多次询问两点之间的最小割 两点间的最小割是这样定义的:原图的每条边有一个割断它的代价,你需要用最小的代价使得这两个点不 ...

  4. C++kruskals算法生成最小协议树(附完整源码)

    C++kruskals算法生成最小协议树 C++kruskals算法生成最小协议树完整源码(定义,实现,main函数测试) C++kruskals算法生成最小协议树完整源码(定义,实现,main函数测 ...

  5. C++prims算法生成最小协议树(附完整源码)

    C++prims算法生成最小协议树 C++实现prims算法生成最小协议树完整源码(定义,实现,main函数测试) C++实现prims算法生成最小协议树完整源码(定义,实现,main函数测试) #i ...

  6. 【数据结构与算法】之深入解析“最小高度树”的求解思路与算法示例

    一.题目要求 树是一个无向图,其中任何两个顶点只通过一条路径连接.换句话说,一个任何没有简单环路的连通图都是一棵树. 给你一棵包含 n 个节点的树,标记为 0 到 n - 1,给定数字 n 和一个有 ...

  7. 洛谷.4897.[模板]最小割树(Dinic)

    题目链接 最小割树模板.具体见:https://www.cnblogs.com/SovietPower/p/9734013.html. ISAP不知为啥T成0分了.. Dinic: //1566ms ...

  8. 【20181102T2】飞越行星带【智商题+最小瓶颈路】

    题面 [正解] 一眼不可做啊 --相当于求路线上穿过的点最小距离最大 最小最大--二分啊 现在相当于给一个直径,要判断这个直径是否能从左边穿到右边 我们可以在距离不超过直径的点连一条边,\(y=0\) ...

  9. P4897 【模板】最小割树(Gomory-Hu Tree)(网络流/最小割/树形结构)

    P4897 [模板]最小割树(Gomory-Hu Tree) 这个算法可以用来求解一个无向图上任意两点的最小割,具体过程就是每次选择两个点求最小割,然后在一个新图中这两个点连边,然后对于这两个点的连通 ...

最新文章

  1. matlab离散系统 响应,离散系统的频率响应和输出响应的matlab实现
  2. Docker安装及配置
  3. Use the Shapes window to organize and find shapes
  4. mysql 5.5.23 winx64,win10下mysql 5.7.23 winx64安装配置方法图文教程
  5. yaml parse python_python-yaml
  6. 域内禁止不明东西连接DHCP
  7. 台式计算机技术方案,2017年4月自考02316计算机应用技术真题及答案
  8. View Agent Direct-Connection安装后,连接黑屏
  9. 【.Net】C#实现多线程的方式:使用Parallel类
  10. Unity基础学习笔记(一)
  11. oracle单行函数 之 字符函数
  12. editorMd插件的使用总结(包括开启图片上传及拖拉粘贴上传图片)
  13. TPU是什么材料,tpu材料属于塑料吗?
  14. 如何用手机在图片上标箭头_如何在手机上快速给图片做标记?
  15. 港股系统开发美股软件开发之简单了解券商交易系统开发及港美股交易平台
  16. 游戏耳机怎么选购?适合玩游戏的无线蓝牙耳机品牌
  17. 【2022应届生的入职感悟】
  18. java 进销存 springmvc SSM crm 项目 系统
  19. 2017-2018-1 20155227 20155318 实验一 开发环境的熟悉
  20. python中average什么意思_用numpy.average()做加权平均,结果是nan?

热门文章

  1. Android-Universal-Image-Loader学习笔记(3)--内存缓存
  2. redhat6.8链路聚合
  3. OData V4 系列 查询操作
  4. DotNetOpenAuth实践之搭建验证服务器
  5. 使用Hamcrest增强JUnit的测试能力
  6. 如何将word中的对象怎么显示到工具栏_MathType怎么添加到Word快速访问栏?
  7. C++ cin.sync()和cin.ignore()
  8. 阿里巴巴的开源项目Druid(关于数据库连接)
  9. JAVA知识学习——类的修饰符
  10. Django 学习笔记第一课