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. mui中的关闭页面的几种方法

    一.总结: mui中关闭当前页面的几种方式: 1.swipeBack(暂未测试过) 2.keyEventBind(暂未测试过) 3.给标签的class加.mui-action-back(返回的是前一个 ...

  2. HackerRank "Poisonous Plants"

    I had the same BFS idea as one of the AC code this: because dying pattern is spead from the initial ...

  3. (转)用Eclipse进行C++开发时Bianry not found的问题解决

    本文转载自:http://blog.csdn.net/baimafujinji/article/details/49722399 由于Visual Studio体积过于庞大,很多人选择在Eclipse ...

  4. 读书笔记:Sheldon Ross:概率论基础教程:随机变量

    例1b 一个坛子里装有编号1-20的球,无放回抽取3个,取出球中至少一个号码大于等于17的概率是多少? 除了书上的解法外,还有一种解法: 考虑相反的情况:三个球的号码都小于17. 第一次从编号1-16 ...

  5. bzoj2338 数矩形

    给出N(N≤1500)个点,求选四个点作为顶点组成矩形的最大面积,保证有解. 对每两个点连边,按边长排序,枚举等长且中点相同的边作为对角线组成矩形,计算面积取最大值. 时间复杂度O(n2logn) # ...

  6. 常用JS

    window.opener.location.reload(); 刷新上一个页面 window.opener=null;window.open('','_self');window.close(); ...

  7. Windows2008 Patching(打补丁)

    我们都知道Windows的服务器都需要打补丁的,要不然漏洞那个叫多啊.Windows的系列服务器打补丁无非就是两种方法: 1. 通过Internet打补丁: Go to control Panel-& ...

  8. android学习笔记48——SQLite

    SQLite SQLite试试一个嵌入式的数据库引擎,专门用于资源有限的设备(如手机.PDA)上适量数据存取. SQLite支持绝大部分SQL92语法,同样允许开发者使用SQL语句操作数据库中的数据, ...

  9. DDD学习笔记二

    参考:感谢博主的分享... http://www.cnblogs.com/netfocus/archive/2012/02/12/2347938.html DDD ==> 领域驱动设计(Doma ...

  10. 如何生成RestFul Api文档

    Web API文档工具列表Swagger ——Swagger框架可以通过代码生成漂亮的在线API,甚至可以提供运行示例.支持Scala.Java.Javascript.Ruby.PHP甚至 Actio ...