1307: City Tour

Time Limit: 1 Sec  Memory Limit: 128 MB

[Submit][Status][Web Board]

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

Sample Output

90
30
15
   思路:这个题出的很好,和13南京网赛的1002很像,容易犯一个小毛病的地方就是加边的时候判断。
#include <iostream>
#include <stdio.h>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <queue>
#include <set>
#include <algorithm>
#include <map>
#include <math.h>
#define Max(a,b) ((a)>(b)?(a):(b))
using namespace std ;
typedef long long LL ;
int N ,M ,Start ,End;
struct Edge{
int u ;
int v ;
int w ;
friend bool operator <(const Edge A ,const Edge B){
return A.w<B.w ;
}
};
Edge edge[] ;
const int size= ;
vector< pair<int,int> >vec[size] ;
int father[size] ;
void init(){
for(int i=;i<=N;i++){
father[i]=i ;
vec[i].clear() ;
}
}
int find_father(int x){
if(father[x]==x)
return x ;
else
return father[x]=find_father(father[x]) ;
}
void read(){
for(int i= ;i<=M ;i++)
scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w) ;
}
int dist[size] ;
bool in_queue[size] ;
const int inf= ;
int spfa(){
queue<int>que ;
fill(dist,dist++N,inf) ;
fill(in_queue,in_queue++N,) ;
in_queue[Start]= ;
dist[Start]= ;
que.push(Start) ;
while(!que.empty()){
int u=que.front() ;
que.pop() ;
in_queue[u]= ;
for(int i=;i<vec[u].size();i++){
int v=vec[u][i].first ;
int w=vec[u][i].second ;
if(dist[u]+w<dist[v]){
dist[v]=dist[u]+w ;
if(!in_queue[v]){
in_queue[v]= ;
que.push(v) ;
}
}
}
}
return dist[End] ;
}
int gao(){
init() ;
read() ;
sort(edge+,edge++M) ;
int u ,v ,w ,f_u ,f_v ,i ,j ;
for(i=;i<=M;i++){
u=edge[i].u ;
v=edge[i].v ;
w=edge[i].w ;
f_u=find_father(u) ;
f_v=find_father(v) ;
if(f_u!=f_v)
father[f_u]=f_v ;
vec[u].push_back(make_pair(v,w)) ;
vec[v].push_back(make_pair(u,w)) ;
f_u=find_father(Start) ;
f_v=find_father(End) ;
if(f_u==f_v)
break ;
}
for(j=i+;j<=M;j++){
if(edge[j].w==edge[i].w){
u=edge[j].u ;
v=edge[j].v ;
w=edge[j].w ;
vec[u].push_back(make_pair(v,w)) ;
vec[v].push_back(make_pair(u,w)) ;
}
else
break ;
}
return spfa() ;
}
int main(){
while(scanf("%d%d%d%d",&N,&M,&Start,&End)!=EOF){
printf("%d\n",gao()) ;
}
return ;
}

1307: City Tour的更多相关文章

  1. HDU 5013 City Tour

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

  2. City Tour

    Description Alice想要从城市A出发到城市B,由于Alice最近比较穷(不像集训队陈兴老师是个rich second),所以只能选择做火车从A到B.不过Alice很讨厌坐火车,火车上人比 ...

  3. CSU-1307-二分+dij

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

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

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

  5. 每日英语:How to Save Detroit

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

  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. Ext JS 5的声明式事件监听

    在前文<在Ext JS 5使用ViewControllers>中,简单的介绍了Ext JS 5的一项重要改进——声明式事件监听.在本文,将深度探讨如何使用声明式事件监听啦简化应用程序的视图 ...

  2. taglib例子

    jsp中的taglib有点类似asp.net中的customer control.自定义标签. 一个最简单的taglib使用例子:检查用户是否已经被登陆. 新建一个class: CheckLoginT ...

  3. 剑指offer系列48---左旋转字符串

    [题目]对于一个给定的字符序列S,旋转指定位置左边的字符到右边.. * 例如,字符序列S=”abcXYZdef”,要求输出循环左移3位后的结果,即“XYZdefabc”. * [思路]先分成两个部分: ...

  4. 【JDBC】向数据表插入数据时,自动获取生成的主键

    数据表设计时,一般都会有一个主键(Key)(自己指定),有时也可以使用联合主键: 有许多数据库提供了隐藏列为表中的每行记录分配一个唯一键值(如:rowid): 当我们没有指定哪一列作为主键key时,数 ...

  5. 使用仓库管理器——Sonatype Nexus的九大理由

    目前有很多组织使用了一些工具依赖于Maven仓库,但他们并没有采用一个仓库管理器,对于这一点我十分惊讶.可能没人提出来这一点,没人站出来告诉别人使用一个仓库管理器能带来什么好处.我经常能从很多不使用M ...

  6. CentOS 7.0体验与之前版本的不同

    RHEL7和CentOS7出来有一段时间了,拿出点时间研究下,有几个地方跟6和5系列相比改变比较大,估计不少童鞋有点不太习惯.下面简要举例说明改变比较大的要点: 一.CentOS的Services使用 ...

  7. android学习笔记14——GridView、ImageSwitcher

    GridView--网格视图.ImageSwitcher--图像切换器 ==> GridView,用于在界面上按行.列的分布形式显示多个组件:GridView和ListView父类相同——Abs ...

  8. 黄聪:wordpress如何使用get_avatar禁止调用gravatar头像,替换为自定义头像

    add_filter( 'get_avatar' , 'my_custom_avatar' , 1 , 5 ); function my_custom_avatar( $avatar, $id_or_ ...

  9. Win7家庭版开启Administrator管理员帐户的方法

    Win7家庭版开启Administrator管理员帐户的方法 发布时间:2014-11-17 18:30:06来源:系统盒浏览数:2786 很多用户安装好Win7系统第一步就是开启Administra ...

  10. 无状态服务(stateless service)

    一.定义 无状态服务(stateless service)对单次请求的处理,不依赖其他请求,也就是说,处理一次请求所需的全部信息,要么都包含在这个请求里,要么可以从外部获取到(比如说数据库),服务器本 ...