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. 关于SqlServer的DBHelper类以及数据分页

    前端: <My:AspNetPager class="arPage" PageSize="20" UrlPaging="true" r ...

  2. 程序员书单_java web编程篇

    李兴华JavaWeb开发实战经典 http://download.csdn.net/detail/shenzhq1980/9137653 Servlet与JSP核心编程 第2版_1 http://do ...

  3. 使用 Windows PowerShell 来管理和开发 windowsazure.cn 账户的特别注意事项

    6月6日,微软面向中国大陆用户开放了Microsoft Azure公众预览版的申请界面.大家可以申请免费的 beta 试用,收到内附邀请码的通知邮件后只需输入激活码即可开始免费试用.具体网址为: ht ...

  4. php时间日期处理

    php日期函数: 首先想到的就是date(),time(),strtotime(),mktime() strtotime() strtotime()函数用于将英文文本字符串表示的日期转换为时间戳,为 ...

  5. android学习笔记15——Galley

    Gallery==>画廊视图 Gallery和Spinnery父类相同——AbsSpinner,表明Garrey和Spinner都是一个列表框. 两者之间的区别是:Spinner显示的是一个垂直 ...

  6. Linux下nl命令的用法详解

    Linux中nl命令和cat命令很像,不过nl命令会打上行号,属于比较不常用的命令,下面随小编一起来了解下这个鲜为人知的nl命令吧. nl命令在linux系统中用来计算文件中行号.nl 可以将输出的文 ...

  7. 验证视图状态 MAC 失败

    起因: 最近在做一个项目需要用到生成多个Html页,采用一下方法动态生成. WebRequest request = WebRequest.Create(pageurl); WebResponse r ...

  8. Python 模块学习:re模块

    今天学习了Python中有关正则表达式的知识.关于正则表达式的语法,不作过多解释,网上有许多学习的资料.这里主要介绍Python中常用的正则表达式处理函数. 方法/属性 作用 match() 决定 R ...

  9. win7下vs2008编译出现C1859错误的处理办法

    昨天,电脑上安装的vs2008 sp1版本编译工程时候出了这样的错了:fatal error C1859: 'Debug\**.pch'.清理并重新编译会临时解决问题,但问题仍然会频发.后面上网找资料 ...

  10. memwatch

    一.简介 memwatch可以跟踪程序中的内存泄漏和错误,能检测双重释放(double-free).错误释放(erroneous free).没有释放的内存(unfreed memory).溢出(Ov ...