Description

Alice想要从城市A出发到城市B,由于Alice最近比较穷(不像集训队陈兴老师是个rich second),所以只能选择做火车从A到B。不过Alice很讨厌坐火车,火车上人比较多,比较拥挤,所以Alice有很严格的要求:火车的相邻两站间的最大距离尽可能的短,这样Alice就可以在停站的时候下车休息一下。当然Alice希望整个旅途比较短。

Input

有多组测试数据。
每组测试数据的第一行有两个整数N,M,A,B(N<=2000, M<=50000, N >=2, A,B<=N),其中N是城市的个数,M是城市间通火车的个数。
A,B是Alice起始的城市与目的地城市,城市的标号从1开始。
接下来的M行每行三个整数u,v,w表示从u到v和从v到u有一条铁路,距离为w, u,v<=N, w<=10000。

Output

对于每组测试数据输出满足Alice要求的从A到B的最短距离。

Sample Input

3 3 1 2
1 2 80
1 3 40
2 3 50
3 3 1 2
1 2 90
1 3 10
2 3 20
4 5 1 4
1 2 8
1 4 9
1 3 10
2 4 7
3 4 8
#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

90 30 15

City Tour的更多相关文章

  1. 1307: City Tour

    1307: City Tour Time Limit: 1 Sec  Memory Limit: 128 MB [Submit][Status][Web Board] Description Alic ...

  2. HDU 5013 City Tour

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5013 题意: 思路: 这里有错,是Hi(x)=sigama(Hji)(j属于x) const int ...

  3. 2013 CSU校队选拔赛(1) 部分题解

    A: Decimal Time Limit: 1 Sec   Memory Limit: 128 MB Submit: 99   Solved: 10 [ Submit][ Status][ Web ...

  4. 每日英语:How to Save Detroit

    Detroit is beautiful-though you probably have to be a child of the industrial Midwest, like me, to s ...

  5. CSU-1307-二分+dij

    1307: City Tour Submit Page   Summary   Time Limit: 1 Sec     Memory Limit: 128 Mb     Submitted: 59 ...

  6. POJ 1637 Sightseeing tour

    Sightseeing tour Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9276   Accepted: 3924 ...

  7. poj1637 Sightseeing tour

    Sightseeing tour Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8859   Accepted: 3728 ...

  8. POJ 1637 Sightseeing tour (混合图欧拉路判定)

    Sightseeing tour Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6986   Accepted: 2901 ...

  9. POJ 1637 Sightseeing tour (混合图欧拉回路)

    Sightseeing tour   Description The city executive board in Lund wants to construct a sightseeing tou ...

随机推荐

  1. MVCC的一种实现方案

    源信息来源:http://my.oschina.net/juliashine/blog/111624 -- 简单描述: 一个data-server,通过mvcc来实现事务的一致性,已支持更高的吞吐和更 ...

  2. CComPtr用法

    COM接口指针很危险,因为使用过程中需要每一个使用者都要严格并且正确的AddRef和Release,一旦出现问题,就会造成对象不能被正常释放,或者对象被重复删除,造成程序崩溃.所以使用COM接口,必须 ...

  3. Sublime Text 常用快捷键

    /* 之前用过的好多的编辑器,从IT大牛们的博客里知道了他们所谓的Vim,Vi,Emacs等,也都挨个装上试了,不尽人意,但自从遇到了Sublime Text,甚是喜欢,有道是“情不知何而起,一往而深 ...

  4. [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 ...

  5. 提高你的Java代码质量吧:让我们疑惑的字符串拼接方式的选择

    一.分析  对于一个字符串进行拼接有三种方法:加号.concat方法.及StringBuiler或StringBuffer. 1."+"方法拼接字符串  str += " ...

  6. TCP总结

      TCP协议   <计算机网络>谢希仁 及笔记 TCP 的那些事儿(上):http://coolshell.cn/articles/11564.html TCP 的那些事儿(下):htt ...

  7. 自定义Toast

    简易自定义Toast public class MainActivity extends ListActivity );//边角         gradientDrawable.setGradien ...

  8. IE7append新的元素自动补充完整路径

    在IE7下,进行append操作时,会把像<img />的src补成完整路径.对于上传到临时目录的图片,提交到后台要进行路径判断的情形要十分注意.

  9. OREACLE 数据库建表 添加判断表是否存在 不存在则新建

    declare  cnt number; begin   ---查询要创建的表是否存在   select count(*)into cnt from user_tables where table_n ...

  10. 你好,C++(27)在一个函数内部调用它自己本身 5.1.5 函数的递归调用

    5.1.5 函数的递归调用 在函数调用中,通常我们都是在一个函数中调用另外一个函数,以此来完成其中的某部分功能.例如,我们在main()主函数中调用PowerSum()函数来计算两个数的平方和,而在P ...