UVA 10816 + HDU 1839 Dijstra + 二分 (待研究)
UVA
题意:两个绿洲之间是沙漠,沙漠的温度不同,告诉起点,终点,求使得从起点到终点的最高温度最小的路径,如果有多条,输出长度最短的路径;
思路:用最小费用(最短路径)最大流(最小温度)也能搞吧,但因为题意是看着博客做的,不小心看到了他的思路,就自己实现了一遍,二分温度,假设当前温度为x,求保证最大温度为x的情况下的最短路;打印路径就是递归打印。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define oo 1e9
#define repu(i, a, b) for(int i = (a); i < (b); i++)
using namespace std;
typedef long long LL;
const int maxn=;
const int INF=0x3f3f3f3f;
int rode[maxn];
int st,ed;
struct Edge
{
int u, v;
double t, d;
Edge(int u, int v, double t, double d):u(u), v(v), t(t), d(d) {}
};
struct qnode
{
int u;
double d;
qnode(int u, double d):u(u), d(d) {}
bool operator < (const qnode a)const
{
return d > a.d;
}
};
struct Dijkstra
{
int n;
vector<int> G[maxn];
vector<Edge> edge;
double d[maxn];
bool vis[maxn];
void init(int n)
{
this->n=n;
for(int i=; i<=n; i++)
{
G[i].clear();
vis[i]=;
d[i]=INF;
}
edge.clear();
}
void AddEdge(int u, int v, double t, double d)
{
G[u].push_back(edge.size());
edge.push_back(Edge(u, v, t, d));
}
bool dijkstra(double cur)
{
memset(rode, -, sizeof rode);
memset(vis, , sizeof vis);
repu(i,,n+)
d[i] = oo;
priority_queue<qnode> q;
d[st]=0.00;
q.push(qnode(st, ));
while(!q.empty())
{
qnode x=q.top();
q.pop();
if(vis[x.u])
continue ;
vis[x.u]=true;
if(x.u == ed)
return true;
for(int i=; i<G[x.u].size(); i++)
{
Edge& e=edge[G[x.u][i]];
if(e.t <= cur&& d[e.v]>d[x.u]+e.d)
{
///加条件,保证最大的温度是e.t
d[e.v]=d[x.u]+e.d;
rode[e.v]=x.u;///保存路径
q.push(qnode(e.v, d[e.v]));
}
}
}
return d[ed] < oo;
}
} dij;
void printroad(int x)
{
if(x==st)
{
printf("%d", x);
return ;
}
printroad(rode[x]);
printf(" %d", x);
}
int main()
{
int n,m;
while(~scanf("%d%d",&m,&n))
{
dij.init(m);
scanf("%d%d",&st,&ed);
int u,v;
double c,w;
vector<double> se;
repu(i,,n)
{
scanf("%d%d%lf%lf",&u,&v,&c,&w);
se.push_back(c);
dij.AddEdge(u, v, c, w);
dij.AddEdge(v, u, c, w);
}
sort(se.begin(),se.end());///按照温度排序进行二分
int L = , R = unique(se.begin(),se.end())- se.begin()-;
int mid;
while(L<R)
{
mid=(L+R)/;
if(dij.dijkstra(se[mid]))
R=mid;///如果存在就找更小的温度
else L=mid+;
}
dij.dijkstra(se[R]);///找到的R就是最终的条件
printroad(ed);
printf("\n%.1lf %.1lf\n", dij.d[ed], se[R]);
}
return ;
}
HDU
题意:起点1,终点m,每条道路都有一个最大容量,以及通过的时间,求在时间T内能过的最大的容量;
思路:得用最大流吧,但是可能数据水,跟上一题一样的思路居然也能过。
注意:说代码的话,跟上一题完全一样,除了二分,但我被坑的一点就是二分,L跟R赋值的时候没改掉,就debug其他的地方,花费了不少的时间;
按照从小到大的顺序排好后,进行二分,注意mid = (l + r + 1)/ 2;符合的 l = mid;否则r = mid -1;(这是找越大的越好)---mid为啥+1母鸡啊
按照从小到大的顺序排好后,进行二分,注意mid = (l + r )/ 2;符合的 l = mid - 1;否则r = mid ;(这是找越小的越好)
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define repu(i, a, b) for(int i = (a); i < (b); i++)
using namespace std;
typedef long long LL;
const int maxn=;
const int INF=0x3f3f3f3f;
int rode[maxn];
int st,ed,T;
struct Edge
{
int u, v;
int t, d;
Edge(int u, int v, int t, int d):u(u), v(v), t(t), d(d) {}
};
struct qnode
{
int u;
int d;
qnode(int u, int d):u(u), d(d) {}
bool operator < (const qnode a)const
{
return d > a.d;
}
};
struct Dijkstra
{
int n;
vector<int> G[maxn];
vector<Edge> edge;
int d[maxn];
bool vis[maxn];
void init(int n)
{
this->n=n;
for(int i=; i<=n; i++)
{
G[i].clear();
vis[i]=;
d[i]=INF;
}
edge.clear();
}
void AddEdge(int u, int v, int t, int d)
{
G[u].push_back(edge.size());
edge.push_back(Edge(u, v, t, d));
}
int dijkstra(int cur)
{
memset(vis, , sizeof vis);
repu(i,,n+)
d[i]=i==?:INF;
priority_queue<qnode> q;
d[st]=;
q.push(qnode(st, ));
while(!q.empty())
{
qnode x=q.top();
q.pop();
if(vis[x.u])
continue ;
vis[x.u]=true;
if(x.u == ed)
return d[ed] <= T;
for(int i=; i<G[x.u].size(); i++)
{
Edge& e=edge[G[x.u][i]];
if(e.t >= cur && d[e.v] > d[x.u]+e.d)
{
///加条件,保证最小的容量是cur
d[e.v]=d[x.u]+e.d;
q.push(qnode(e.v, d[e.v]));
}
}
}
return d[ed] <= T;
}
} dij;
int main()
{
int n,m,I;
scanf("%d",&I);
while(I--)
{
scanf("%d%d%d",&m,&n,&T);
dij.init(m);
st = ;
ed = m;
int u,v;
int c,w;
vector<int> se;
repu(i,,n)
{
scanf("%d%d%d%d",&u,&v,&c,&w);
se.push_back(c);
dij.AddEdge(u, v, c, w);
dij.AddEdge(v, u, c, w);
}
sort(se.begin(),se.end());///按照容量排序进行二分
int L = , R = unique(se.begin(),se.end())- se.begin()-;
int mid;
while(L < R)///居然在这里栽坑了
{
mid=(L + R + )/;
if(dij.dijkstra(se[mid]))
L=mid;///如果存在就找更大的容量
else R=mid-;
}
printf("%d\n", se[L]);
}
return ;
}
UVA 10816 + HDU 1839 Dijstra + 二分 (待研究)的更多相关文章
- hdu 1839 Delay Constrained Maximum Capacity Path 二分/最短路
Delay Constrained Maximum Capacity Path Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu. ...
- uva 10004 Bicoloring(dfs二分染色,和hdu 4751代码差不多)
Description In the ``Four Color Map Theorem" was proven with the assistance of a computer. This ...
- hdu 1839 Delay Constrained Maximum Capacity Path(spfa+二分)
Delay Constrained Maximum Capacity Path Time Limit: 10000/10000 MS (Java/Others) Memory Limit: 65 ...
- HDU 1839
http://acm.hdu.edu.cn/showproblem.php?pid=1839 题意:从1到n,要求时间小于等于T到达.每条边有一个容量,问最多能运多少货物. 分析:最多能运的货物取决于 ...
- UVa 714 Copying Books(二分)
题目链接: 传送门 Copying Books Time Limit: 3000MS Memory Limit: 32768 KB Description Before the inventi ...
- hdu 2413(最大匹配+二分)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2413 思路:由于要求最少的时间,可以考虑二分,然后就是满足在limit时间下,如果地球战舰数目比外星战 ...
- HDU 5884 Sort (二分)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5884 nn个有序序列的归并排序.每次可以选择不超过kk个序列进行合并,合并代价为这些序列的长度和.总的 ...
- UVA 10668 - Expanding Rods(数学+二分)
UVA 10668 - Expanding Rods 题目链接 题意:给定一个铁棒,如图中加热会变成一段圆弧,长度为L′=(1+nc)l,问这时和原来位置的高度之差 思路:画一下图能够非常easy推出 ...
- hdu 1281棋盘游戏(二分匹配)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1281 Problem Description 小希和Gardon在玩一个游戏:对一个N*M的棋盘, ...
随机推荐
- ListView实现Item上下拖动交换位置 并且实现下拉刷新 上拉加载更多
ListView实现Item上下拖动交换位置 并且实现下拉刷新 上拉加载更多 package com.example.ListViewDragItem; import android.app.Ac ...
- 【noip新手入门向】OpenJudge1.3-14大象喝水
一.写在前面 我也不知道我为什么要写这个鬼畜的东西←_←才不是为了水blog量什么的(划掉),其实是为了明天给学弟学妹们传教准备. 这道题对完全对c语言没有概念的小萌新们极度友好,可以锻炼小萌新们的代 ...
- Intent意图
1.显式Intent button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(Vie ...
- 第九天 内容提供者 ContentResolver
重点:理解ContentProvider 的作用和创建流程 1. 内容提供者,提供 其他数据库的访问. 特点 - 描述 : 它是android 四大组件之一,需要androidManife ...
- TextInfo
https://msdn.microsoft.com/en-us/library/system.globalization.textinfo.totitlecase(v=vs.110).aspx Co ...
- ie6兼容之绝对定位元素内容为空时高度问题
正常显示: ie6下显示: line6元素高度最小16px; 解决办法: 添加内容在空的div里,并且设置行高即可. 其中,非ie6浏览器不需要再空的div里加无谓的内容,再次需要用“条件注释”来解决 ...
- IIS7配置PHP运行环境
1.下载PHP运行包,网址:http://windows.php.net/download/ 选择的对应的版本包下载,并解压到相应的目录中(不限定),如:E:\vss\php\php_sdk 2.配置 ...
- LTE Module User Documentation(翻译15)——示例程序、参考场景以及故障检测和调试技巧
LTE用户文档 (如有不当的地方,欢迎指正!) 21 Examples Programs(示例程序) 路径 src/lte/examples/ 包含一些示例仿真程序,这些例子表明如何仿真不 ...
- WCF服务在类库中的引用
在类库中引用了WCF服务,悲剧降临了,追踪日志看到下边一串: --------------------------------------------------------------------- ...
- 微信、QQ浏览器X5内核问题汇总
一. 资料汇总 1.前端H5调起QQ浏览器的总结:http://km.oa.com/group/22486/articles/show/210189?kmref=search 2.Android We ...