畅通工程续

Time Limit: 1000ms
Memory Limit: 32768KB

This problem will be judged on HDU. Original ID: 1874
64-bit integer IO format: %I64d      Java class name: Main

 
某省自从实行了很多年的畅通工程计划后,终于修建了很多路。不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多。这让行人很困扰。

现在,已知起点和终点,请你计算出要从起点到终点,最短需要行走多少距离。

 

Input

本题目包含多组数据,请处理到文件结束。
每组数据第一行包含两个正整数N和M(0<N<200,0<M<1000),分别代表现有城镇的数目和已修建的道路的数目。城镇分别以0~N-1编号。
接下来是M行道路信息。每一行有三个整数A,B,X(0<=A,B<N,A!=B,0<X<10000),表示城镇A和城镇B之间有一条长度为X的双向道路。
再接下一行有两个整数S,T(0<=S,T<N),分别代表起点和终点。

 

Output

对于每组数据,请在一行里输出最短需要行走的距离。如果不存在从S到T的路线,就输出-1.

 

Sample Input

3 3
0 1 1
0 2 3
1 2 1
0 2
3 1
0 1 1
1 2

Sample Output

2
-1

Source

 
解题:很水的题目!纯粹是为了学习下Dijkstra算法的优先队列优化。。哎 错了好多次!我逗逼了。。。
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
int n,m,mp[maxn][maxn],d[maxn],s,t;
bool done[maxn];
priority_queue< pii,vector< pii > ,greater< pii > >q;
void dijkstra(int s){
while(!q.empty()) q.pop();
int i,j,u,v;
for(i = ; i < n; i++){
done[i] = false;
d[i] = INF;
}
d[s] = ;
q.push(make_pair(d[s],s));
while(!q.empty()){
u = q.top().second;
q.pop();
if(done[u]) continue;
done[u] = true;
for(v = ; v < n; v++){
if(d[u] < INF && mp[u][v] < INF && d[v] > d[u]+mp[u][v]){
d[v] = d[u]+mp[u][v];
q.push(make_pair(d[v],v));
}
}
if(done[t]) return;
}
}
int main() {
int i,j,u,v,w;
while(~scanf("%d %d",&n,&m)){
for(i = ; i < n; i++){
for(j = ; j < n; j++)
mp[i][j] = INF;
}
for(i = ; i < m; i++){
scanf("%d %d %d",&u,&v,&w);
if(mp[u][v] > w) mp[u][v] = mp[v][u] = w;
}
scanf("%d %d",&s,&t);
dijkstra(s);
printf("%d\n",d[t] == INF?-:d[t]);
}
return ;
}
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
int n,m,s,t,d[maxn],mp[maxn][maxn];
bool done[maxn];
void spfa(int s){
int i,j,u,v;
queue<int>q;
for(i = ; i < n; i++)
d[i] = INF;
memset(done,false,sizeof(done));
d[s] = ;
q.push(s);
done[s] = true;
while(!q.empty()){
u = q.front();
q.pop();
done[u] = false;
for(v = ; v < n; v++){
if(d[v] > d[u]+mp[u][v]){
d[v] = d[u]+mp[u][v];
if(!done[v]){
done[v] = true;
q.push(v);
}
}
}
}
}
int main(){
int i,j,u,v,w;
while(~scanf("%d %d",&n,&m)){
for(i = ; i < n; i++)
for(j = ; j < n; j++)
mp[i][j] = INF;
for(i = ; i < m; i++){
scanf("%d %d %d",&u,&v,&w);
if(mp[u][v] > w) mp[u][v] = mp[v][u] = w;
}
scanf("%d %d",&s,&t);
spfa(s);
printf("%d\n",d[t] == INF?-:d[t]);
}
return ;
}

BNUOJ 6023 畅通工程续的更多相关文章

  1. 畅通工程续——E

    E. 畅通工程续 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多.这让 ...

  2. HDU 1874 畅通工程续(最短路/spfa Dijkstra 邻接矩阵+邻接表)

    题目链接: 传送门 畅通工程续 Time Limit: 1000MS     Memory Limit: 65536K Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路. ...

  3. ACM: HDU 1874 畅通工程续-Dijkstra算法

    HDU 1874 畅通工程续 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Desc ...

  4. hdu 1874 畅通工程续

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1874 畅通工程续 Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过 ...

  5. hdu 1874 畅通工程续 Dijkstra

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1874 题目分析:输入起点和终点,顶点的个数,已连通的边. 输出起点到终点的最短路径,若不存在,输出-1 ...

  6. hdoj 1874 畅通工程续【dijkstra算法or spfa算法】

    畅通工程续 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  7. 畅通工程续 HDOJ--1874

    畅通工程续 Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submiss ...

  8. HDU-1874 畅通工程续 (最短路径启蒙题)

    hdu 1874比较基础,拿来练各种刚学会的算法比较好,可以避免好多陷阱,典型的最短路模板题 畅通工程续 Time Limit: 3000/1000 MS (Java/Others)    Memor ...

  9. 畅通工程续--hdu1874

    畅通工程续 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

随机推荐

  1. 1.1.2最小生成树(Kruskal和Prim算法)

    部分内容摘自 勿在浮沙筑高台 http://blog.csdn.net/luoshixian099/article/details/51908175 关于图的几个概念定义: 连通图:在无向图中,若任意 ...

  2. 洛谷 P1501 [国家集训队]Tree II

    看来这个LCT板子并没有什么问题 #include<cstdio> #include<algorithm> using namespace std; typedef long ...

  3. WebSphere中数据源连接池太小导致的连接超时错误记录

    WebSphere中数据源连接池太小导致的连接超时错误记录. 应用连接超时错误信息: [// ::: CST] webapp E com.ibm.ws.webcontainer.webapp.WebA ...

  4. Maven环境搭建操作记录

    Maven官方网站: http://maven.apache.org/index.html Maven下载地址: http://maven.apache.org/download.cgi Maven历 ...

  5. 2、IO流的分类和IO流体系

  6. XmlDocument

    XmlDocument增删改查. using System; using System.Collections.Generic; using System.ComponentModel; using ...

  7. JAVA300集笔记

    章节2 java入门阶段 2.1注释 单行注释 //  多行注释 /* 内容*/ 文本注释/**内容*/ 注释是为了方便阅读代码,在编译时注释会被删除. 2.2 标识符 标识符作用: 标识符用来给变量 ...

  8. hihocoder1710 等差子数列

    思路: 将数列合并之后使用线段树.边界条件容易写错. 实现: #include <bits/stdc++.h> using namespace std; ; const int INF = ...

  9. bootstrap基本布局

    中文API bootstrap.cn   HTML5文档 类型   移动设备优先 width 属性控制设备的宽度.设置为 device-width 确保它能正确呈现在不同设备上. initial-sc ...

  10. 关于react native在window下运行安卓的时候报 could not connect to development server

    当出现这种问题是网上的解答方案都是一模一样的! 我这边先给个地址讲的是解决方案http://blog.csdn.net/qq_25827845/article/details/52974991 但是我 ...