题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=1874

畅通工程续

Description

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

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

Input

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

单源最短路。。

 #include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<map>
using std::cin;
using std::cout;
using std::endl;
using std::find;
using std::sort;
using std::pair;
using std::vector;
using std::multimap;
using std::priority_queue;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) decltype((c).begin())
#define cls(arr,val) memset(arr,val,sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = ;
struct P {
int w, v;
P(int i = , int j = ) :w(i), v(j) {}
inline bool operator<(const P &a) const {
return w > a.w;
}
};
struct Node { int to, w, next; };
struct Dijkstra {
Node G[N];
int tot, u, v, w, dist[N], head[N];
inline void init() {
tot = ;
cls(head, -), cls(dist, 0x3f);
}
inline void add_edge(int u, int v, int w) {
G[tot] = { v, w, head[u] }; head[u] = tot++;
}
inline void built(int m) {
rep(i, m) {
scanf("%d %d %d", &u, &v, &w);
add_edge(u, v, w), add_edge(v, u, w);
}
scanf("%d %d", &u, &v);
}
inline void dijkstra() {
dist[u] = ;
priority_queue<P> q;
q.push(P(, u));
while (!q.empty()) {
P t = q.top(); q.pop();
int x = t.v;
if (dist[x] < t.w) continue;
for (int i = head[x]; ~i; i = G[i].next) {
int &d = dist[G[i].to];
if (d > dist[x] + G[i].w) {
d = dist[x] + G[i].w;
q.push(P(d, G[i].to));
}
}
}
printf("%d\n", dist[v] == (int)0x3f3f3f3f ? - : dist[v]);
}
}go;
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int n, m;
while (~scanf("%d %d", &n, &m)) {
go.init();
go.built(m);
go.dijkstra();
}
return ;
}

hdu 1874 畅通工程续的更多相关文章

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

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

  2. HDU 1874 畅通工程续-- Dijkstra算法详解 单源点最短路问题

    参考 此题Dijkstra算法,一次AC.这个算法时间复杂度O(n2)附上该算法的演示图(来自维基百科): 附上:  迪科斯彻算法分解(优酷) problem link -> HDU 1874 ...

  3. HDU 1874畅通工程续(迪杰斯特拉算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1874 畅通工程续 Time Limit: 3000/1000 MS (Java/Others)     ...

  4. hdu 1874 畅通工程续 Dijkstra

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

  5. HDU 1874 畅通工程续【Floyd算法实现】

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

  6. hdu 1874 畅通工程续(求最短距离,dijkstra,floyd)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1874 /************************************************* ...

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

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

  8. hdu 1874 畅通工程续(迪杰斯特拉优先队列,floyd,spfa)

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

  9. HDU——1874畅通工程续(Dijkstra与SPFA)

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

随机推荐

  1. 【教程】【FLEX】#002 请求服务端数据(UrlLoader)

    为什么Flex需要请求服务端读取数据,而不是自己读取? Flex 是一门界面语言,主要是做界面展示的,它能实现很多绚丽的效果,这个是传统Web项目部能比的. 但是它对数据库和文件的读写 没有良好的支持 ...

  2. php 5.6.14手动安装 php -v 显示没有安装

    奇怪了,今天利用源码手动php,安装成功后,利用php -v提示没有安装,which php也是有问题,php文件也没有办法执行 搞了半天,发现是没有添加环境变量,╮(╯▽╰)╭ 方法: 修改/etc ...

  3. 关于MPEG2中的图像序列和图像组头GOP

    图像序列 图像序列是由图像组构成的,是随机存取段落. sequence_header_code – The sequence_header_code is the bit string ‘000001 ...

  4. ios开发错误笔记

    今天的奇葩错误,最后解决方式是删除了手机上面的快捷方式,然后再clean,然后再重启了xcode.无语了,xcode也经常出些奇葩问题,真无语啊. ios技术交流群:378501081..期待你加入. ...

  5. sass mapsource --->gulp

    详细,请戳这里 1.安装插件 npm install --save-dev gulp-sass gulp-sourcemaps gulp-autoprefixer 如果安装错误,请用sudo 权限: ...

  6. Leetcode027. Remove Element

    //water class Solution { public: int removeElement(vector<int>& nums, int val) { for(vecto ...

  7. verilog中符号位的扩展问题

    以下内容转自 艾米电子 - 使用有符号数,Verilog(http://www.cnblogs.com/yuphone/archive/2010/12/12/1903647.html) Verilog ...

  8. JS常用的设计模式(11)—— 中介者模式

    中介者对象可以让各个对象之间不需要显示的相互引用,从而使其耦合松散,而且可以独立的改变它们之间的交互. 打个比方,军火买卖双方为了安全起见,找了一个信任的中介来进行交易.买家A把钱交给中介B,然后从中 ...

  9. ASP.NET 项目 App_Code下无法找到类

    APP_CODE 默认情况下,VS2010中新建的WebApplication中是没有App_Code文件夹的,若需要使用,可以自己手动添加文件夹,然后将文件夹名称设置为App_Code,然后在该文件 ...

  10. Visitor

    #include <iostream> #include <vector> using namespace std; #define DESTROY_POINTER(ptr) ...