题目连接

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. 如何由jdk的安装版本改成非安装版本

    背景. 官网一般只提供windows下的exe文件,不提供zip打包文件.有些不愿意使用安装版本. 解决方法 本文以windows 7下安装jdk-6u35-windows-x64.exe为例说明 1 ...

  2. 洛谷P2733 家的范围 Home on the Range

    P2733 家的范围 Home on the Range• o 26通过o 61提交• 题目提供者该用户不存在• 标签USACO• 难度普及+/提高 提交 讨论 题解 最新讨论• 暂时没有讨论题目背景 ...

  3. Android基础总结(2)——活动Activity

    1.什么是活动(Activity) 活动(Activity)是一种可以包含用户界面的组件,主要用于和用户进行交互.一个应用程序中可以包含零个或多个活动,但不包含任何活动的应用程序很少见. 2.怎么使用 ...

  4. iptable软路由

    http://xstarcd.github.io/wiki/Linux/iptables_forward_internetshare.html # 默认丢弃转发,所有内网流量不能访问外网卡 iptab ...

  5. TRMM数据的读取

    编辑下头文件,就有投影信息,然后再转换成你需要的投影方式1) From the command line, type in, ENVI, 2) Click File|Open External Fil ...

  6. 修改VS解决方案及工程名,解决如何打开高/版本VS项目

    对于VS2008等低版本与高版本VS之间的转换问题: 对照下面2个版本的不同点自由修改,切换到相应的版本文件(红字修改,灰色删除) ---------------------------------- ...

  7. 会"说话"的勒索病毒Cerber

    最近有个案子与勒索病毒有关,证物是个台式机,运行Windows 7 64bit操作系统,委托方是某高科技公司,希望能调查出事发的关键时间点.感染来源及途径.恶意程序文件名等相关信息. 在对证物计算机进 ...

  8. .NET中使用log4net

    一,加载log4net引用 下载log4net.dll,我们这里使用的是.NET2.0 下载地址:http://files.cnblogs.com/gosky/log4net-1.2.13-bin-n ...

  9. MySQL中的数据类型

    文本 CHAR(*):最多255个字节的定长字符串,它的长度必须在创建时指定 VARCHAR(*):最多255个字节的可变长度字符串,它的长度必须在创建时指定 TEXT:最大长度为64K字符的变长文本 ...

  10. 建立交叉编译环境(arm-linux-gcc)

    linux系统内核版本:2.6.32-358.el6.x86_64(在64位系统上安装32位程序需要另外安装一些库) arm-linux-gcc版本:本文安装的是友善之臂tiny6410光盘中自带的a ...