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. java 输入输出项目

    package hellohe; import java.util.Scanner; /** * * @author Administrator *1.导入java.util.scanner; *2. ...

  2. 05 Linux下开发JSP项目(Hello world)

    测试环境: 主机系统:Win 7 虚拟机:VMware workstation 11.1.0 虚拟机OS: centos 6.5 64位 Kernel 2.6.32-431-e16.x86_64 My ...

  3. thinkphp禁止模版标签解析

    场景: 页面中某些样式或者js中含有tp定义的模版标签,如果被tp当成模版标签解析,就会解析异常. tp中提供了<literal></literal>标签用于禁止标签内部的代码 ...

  4. js实现windows扫雷(jquery)

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  5. datagridview 不显示行号的问题

    环境:C#,Winform 场景: 窗体上有两个tab页A.B,每个tab页上都有一个DatagridView.窗体加载后,显示tab A选项卡.序号正常显示,但点击B选项卡后,DatagridVie ...

  6. 11个实用的Apache .htaccess配置

    <IfModule mod_rewrite.c>RewriteEngine onRewriteBase /RewriteRule cat_([0-9]{1,})_([0-9]{1,})_( ...

  7. .NET简谈构件系统开发模式

    转自[王清培] http://www.cnblogs.com/wangiqngpei557/archive/2011/06/14/2080416.html 在本人的“.NET简谈插件系统开发模式”一文 ...

  8. mapreduce计算框架

    一. MapReduce执行过程 分片: (1)对输入文件进行逻辑分片,划分split(split大小等于hdfs的block大小) (2)每个split分片文件会发往不同的Mapper节点进行分散处 ...

  9. 不用配置tnsnames.ora,直接通过PL/SQL访问远程数据库

  10. [转]将Word转(保存)为带书签的PDF

    提到的方法非常管用,感谢原作者的分享. 原文地址:http://blog.163.com/rongting_chen/blog/static/16490684420114266192887/ 将wor ...