1307: City Tour
1307: City Tour
Time Limit: 1 Sec Memory Limit: 128 MB
Description
Input
Output
对于每组测试数据输出满足Alice要求的从A到B的最短距离。
Sample Input
Sample Output
#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的更多相关文章
- HDU 5013 City Tour
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5013 题意: 思路: 这里有错,是Hi(x)=sigama(Hji)(j属于x) const int ...
- City Tour
Description Alice想要从城市A出发到城市B,由于Alice最近比较穷(不像集训队陈兴老师是个rich second),所以只能选择做火车从A到B.不过Alice很讨厌坐火车,火车上人比 ...
- CSU-1307-二分+dij
1307: City Tour Submit Page Summary Time Limit: 1 Sec Memory Limit: 128 Mb Submitted: 59 ...
- 2013 CSU校队选拔赛(1) 部分题解
A: Decimal Time Limit: 1 Sec Memory Limit: 128 MB Submit: 99 Solved: 10 [ Submit][ Status][ Web ...
- 每日英语:How to Save Detroit
Detroit is beautiful-though you probably have to be a child of the industrial Midwest, like me, to s ...
- POJ 1637 Sightseeing tour
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9276 Accepted: 3924 ...
- poj1637 Sightseeing tour
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8859 Accepted: 3728 ...
- POJ 1637 Sightseeing tour (混合图欧拉路判定)
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6986 Accepted: 2901 ...
- POJ 1637 Sightseeing tour (混合图欧拉回路)
Sightseeing tour Description The city executive board in Lund wants to construct a sightseeing tou ...
随机推荐
- java 输入输出项目
package hellohe; import java.util.Scanner; /** * * @author Administrator *1.导入java.util.scanner; *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 ...
- thinkphp禁止模版标签解析
场景: 页面中某些样式或者js中含有tp定义的模版标签,如果被tp当成模版标签解析,就会解析异常. tp中提供了<literal></literal>标签用于禁止标签内部的代码 ...
- js实现windows扫雷(jquery)
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- datagridview 不显示行号的问题
环境:C#,Winform 场景: 窗体上有两个tab页A.B,每个tab页上都有一个DatagridView.窗体加载后,显示tab A选项卡.序号正常显示,但点击B选项卡后,DatagridVie ...
- 11个实用的Apache .htaccess配置
<IfModule mod_rewrite.c>RewriteEngine onRewriteBase /RewriteRule cat_([0-9]{1,})_([0-9]{1,})_( ...
- .NET简谈构件系统开发模式
转自[王清培] http://www.cnblogs.com/wangiqngpei557/archive/2011/06/14/2080416.html 在本人的“.NET简谈插件系统开发模式”一文 ...
- mapreduce计算框架
一. MapReduce执行过程 分片: (1)对输入文件进行逻辑分片,划分split(split大小等于hdfs的block大小) (2)每个split分片文件会发往不同的Mapper节点进行分散处 ...
- 不用配置tnsnames.ora,直接通过PL/SQL访问远程数据库
- [转]将Word转(保存)为带书签的PDF
提到的方法非常管用,感谢原作者的分享. 原文地址:http://blog.163.com/rongting_chen/blog/static/16490684420114266192887/ 将wor ...