City Tour
Description
Input
Output
对于每组测试数据输出满足Alice要求的从A到B的最短距离。
Sample Input
#include<stdio.h>
#include<string.h>
#define maxn 2020
#define maxm 50500
#define inf 100000000 struct edge
{
int u,v;
int val,next;
} e[*maxm],seq[maxm]; int last[maxn];
int que[maxm],dis[maxn];
bool vis[maxn];
int t,n,m,A,B; void add(int u,int v,int val)//离散化存储
{
e[t].v = v;
e[t].val = val;
e[t].next = last[u];
last[u] = t++;
return ;
}
void build(int val)//建树
{
memset(last,-,sizeof(last));
t = ;
for (int i=; i<=m; i++)
if (seq[i].val <= val)
{
add(seq[i].u,seq[i].v,seq[i].val);
add(seq[i].v,seq[i].u,seq[i].val);
}
return ;
} int spfa()
{//采用负权存储
memset(vis,,sizeof(vis));
for (int i=; i<=n; i++) dis[i] = inf;
int head = , tail = ;
que[++tail] = A;
vis[A] = ;
dis[A] = ;
while (head < tail)
{
int u = que[++head];
for (int j=last[u]; j!=-; j=e[j].next)//遍历与j相连的边
{
int v = e[j].v;
int val = e[j].val;
if (dis[u] + val < dis[v]) //松弛条件判断
{
dis[v] = dis[u] + val;
if (vis[v]==)
{
vis[v] = ;
que[++tail] = v;
}
}
}
vis[u] = ;
}
if (dis[B] < inf) return dis[B];
return -;
}
int solve()
{
int res = -;
int l = , r = ;
while (l <= r)//二分最大边
{
int mid = (l + r)/;
build(mid);
int tmp = spfa();
if (tmp == -) l = mid + ;
else
{//当mid(即每段中的最大值)减少时,res进行更新
res = tmp;
r = mid - ;
}
}
return res;
}
int main()
{
while (scanf("%d%d%d%d",&n,&m,&A,&B)==)
{
for (int i=; i<=m; i++) scanf("%d%d%d",&seq[i].u,&seq[i].v,&seq[i].val);
int ans = solve();
printf("%d\n",ans);
}
return ;
}
图论(二分+SPFA)
Sample Output
City Tour的更多相关文章
- 1307: City Tour
1307: City Tour Time Limit: 1 Sec Memory Limit: 128 MB [Submit][Status][Web Board] Description Alic ...
- HDU 5013 City Tour
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5013 题意: 思路: 这里有错,是Hi(x)=sigama(Hji)(j属于x) const int ...
- 2013 CSU校队选拔赛(1) 部分题解
A: Decimal Time Limit: 1 Sec Memory Limit: 128 MB Submit: 99 Solved: 10 [ Submit][ Status][ Web ...
- 每日英语:How to Save Detroit
Detroit is beautiful-though you probably have to be a child of the industrial Midwest, like me, to s ...
- CSU-1307-二分+dij
1307: City Tour Submit Page Summary Time Limit: 1 Sec Memory Limit: 128 Mb Submitted: 59 ...
- POJ 1637 Sightseeing tour
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9276 Accepted: 3924 ...
- poj1637 Sightseeing tour
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8859 Accepted: 3728 ...
- POJ 1637 Sightseeing tour (混合图欧拉路判定)
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6986 Accepted: 2901 ...
- POJ 1637 Sightseeing tour (混合图欧拉回路)
Sightseeing tour Description The city executive board in Lund wants to construct a sightseeing tou ...
随机推荐
- MVCC的一种实现方案
源信息来源:http://my.oschina.net/juliashine/blog/111624 -- 简单描述: 一个data-server,通过mvcc来实现事务的一致性,已支持更高的吞吐和更 ...
- CComPtr用法
COM接口指针很危险,因为使用过程中需要每一个使用者都要严格并且正确的AddRef和Release,一旦出现问题,就会造成对象不能被正常释放,或者对象被重复删除,造成程序崩溃.所以使用COM接口,必须 ...
- Sublime Text 常用快捷键
/* 之前用过的好多的编辑器,从IT大牛们的博客里知道了他们所谓的Vim,Vi,Emacs等,也都挨个装上试了,不尽人意,但自从遇到了Sublime Text,甚是喜欢,有道是“情不知何而起,一往而深 ...
- [Angular 2] Using a Value from the Store in a Reducer
RxJS allows you to combine streams in various ways. This lesson shows you how to take a click stream ...
- 提高你的Java代码质量吧:让我们疑惑的字符串拼接方式的选择
一.分析 对于一个字符串进行拼接有三种方法:加号.concat方法.及StringBuiler或StringBuffer. 1."+"方法拼接字符串 str += " ...
- TCP总结
TCP协议 <计算机网络>谢希仁 及笔记 TCP 的那些事儿(上):http://coolshell.cn/articles/11564.html TCP 的那些事儿(下):htt ...
- 自定义Toast
简易自定义Toast public class MainActivity extends ListActivity );//边角 gradientDrawable.setGradien ...
- IE7append新的元素自动补充完整路径
在IE7下,进行append操作时,会把像<img />的src补成完整路径.对于上传到临时目录的图片,提交到后台要进行路径判断的情形要十分注意.
- OREACLE 数据库建表 添加判断表是否存在 不存在则新建
declare cnt number; begin ---查询要创建的表是否存在 select count(*)into cnt from user_tables where table_n ...
- 你好,C++(27)在一个函数内部调用它自己本身 5.1.5 函数的递归调用
5.1.5 函数的递归调用 在函数调用中,通常我们都是在一个函数中调用另外一个函数,以此来完成其中的某部分功能.例如,我们在main()主函数中调用PowerSum()函数来计算两个数的平方和,而在P ...