题目连接

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. eclipse 拨打电话、拨号,发短信

    1.拨打电话,拨号 //拨打电话Intent intent = new Intent(); intent.setAction(Intent.ACTION_CALL); intent.setData(U ...

  2. Django中级篇之Model专题

    ORM 就是用面向对象的方式去操作数据库的创建表以及增删改查等操作 到目前为止,当我们的程序涉及到数据库相关操作时,我们一般都会这么搞: 创建数据库,设计表结构和字段 使用 MySQLdb 来连接数据 ...

  3. Oracle密码过期the password has expired

    oracle11G新特征,半年会提醒一次修改新密码 正常情况:只要输入原先帐号密码,回车即可. 不正常情况:输入新密码帐号,回车,提示验证错误,这时就需要切换到没过期的管理员用户,高权限也行,输入   ...

  4. 创投女王徐新:如何迅速做到细分市场第一(FW)

    http://mp.weixin.qq.com/s?__biz=MjM5NzUxNTQ2Mw==&mid=203368116&idx=2&sn=e586a2f28dfb2b50 ...

  5. ASPxGridView中批量提交及个别提交的写法

    //获取chech box ID protected string GetProtoID() { string protoId = ""; //获取选中的记录Id List< ...

  6. IE 下的z-index BUG问题

    今天做项目的时候在IE7 碰到了IE 的 z-index 问题: 然后问了几个朋友,总结了一下z-index问题的几个要点: 1.z-index 是要在position为relative或者absol ...

  7. angular $apply()以及$digest()讲解1

    一些知名的批评和缺陷.他们都涉及到$digest loop(更新周期)中一个很常见的问题:如何在Angular之外更新$scope? 在哪调用 $apply? 更佳的做法是确保你是在$digest l ...

  8. Java 单测 回滚

    Java 在单测的时候 需要做回滚 设置如下: 需要添加以下 注解 在类上 defaultRollback = true : 为 默认全部回滚 defaultRollback = false : 为 ...

  9. java初始化深度剖析

    当用一个类来创建对象的时候,JVM会帮我们执行下面的6个步骤. 1.在磁盘上找到对应内的class文件: 2.加载class文件到内存,初始化所有静态域(成员变量和方法): 3.如果使用new cla ...

  10. 无法运行maven项目

    tomcat Server Location 选择 User Tomcat installation 设置CATALINA_HOME环境变量(tomcat start.bat启动不了)1.CATALI ...