hdu-oj 1874 畅通工程续
最短路基础
这个题目hdu-oj 1874可以用来练习最短路的一些算法。
Dijkstra 无优化版本
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#define mt memset
using namespace std;
const int maxn = 210;
const int inf = 1<<30;
int N,M;
int mp[maxn][maxn];
int cost[maxn];
bool vis[maxn];
void dijkstra(int s, int t) {
mt(vis, 0, sizeof(vis));
for(int i = 0; i < N; i++){
cost[i] = (i == s? 0 :mp[s][i]);
}
vis[s] = true;
for(int i = 1; i < N; i++) {
int _min = inf, p = -1;
for(int j = 0; j < N; j++) {
if(!vis[j] && _min > cost[j]) {
_min = cost[j];
p = j;
}
}
if(p == -1 || _min == inf) continue;//这是重点
vis[p] = true;
for(int j = 0; j < N; j++) {
if(!vis[j] && cost[j] > cost[p]+mp[p][j]) {
cost[j] = cost[p]+mp[p][j];
}
}
}
}
int main() {
while(~scanf("%d%d", &N, &M)) {
for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
mp[i][j] = inf;
}
}
for(int i = 0; i < M; i++) {
int a, b, x;
scanf("%d%d%d", &a, &b, &x);
mp[a][b] = mp[b][a] = min(mp[a][b], x);
}
int s,t;
scanf("%d%d", &s, &t);
dijkstra(s,t);
printf("%d\n", cost[t] == inf? -1: cost[t]);
}
return 0;
}
未优化版的Dijkstra算法时间复杂度为O(n^2)
未完待续...
hdu-oj 1874 畅通工程续的更多相关文章
- ACM: HDU 1874 畅通工程续-Dijkstra算法
HDU 1874 畅通工程续 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Desc ...
- hdu 1874 畅通工程续
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1874 畅通工程续 Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过 ...
- HDU 1874畅通工程续(迪杰斯特拉算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1874 畅通工程续 Time Limit: 3000/1000 MS (Java/Others) ...
- HDU 1874 畅通工程续-- Dijkstra算法详解 单源点最短路问题
参考 此题Dijkstra算法,一次AC.这个算法时间复杂度O(n2)附上该算法的演示图(来自维基百科): 附上: 迪科斯彻算法分解(优酷) problem link -> HDU 1874 ...
- hdu 1874 畅通工程续 Dijkstra
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1874 题目分析:输入起点和终点,顶点的个数,已连通的边. 输出起点到终点的最短路径,若不存在,输出-1 ...
- HDU 1874 畅通工程续【Floyd算法实现】
畅通工程续 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- hdu 1874 畅通工程续(求最短距离,dijkstra,floyd)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1874 /************************************************* ...
- HDU 1874 畅通工程续(最短路/spfa Dijkstra 邻接矩阵+邻接表)
题目链接: 传送门 畅通工程续 Time Limit: 1000MS Memory Limit: 65536K Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路. ...
- hdu 1874 畅通工程续(迪杰斯特拉优先队列,floyd,spfa)
畅通工程续 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- HDU——1874畅通工程续(Dijkstra与SPFA)
畅通工程续 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submiss ...
随机推荐
- oc知道经纬度求位置
CLLocation *newLocation = [locations lastObject]; CLGeocodeCompletionHandler handler = ^(NSArray *pl ...
- 17 个 tar 命令实用示例【转】
Tar(Tape ARchive,磁带归档的缩写,LCTT 译注:最初设计用于将文件打包到磁带上,现在我们大都使用它来实现备份某个分区或者某些重要的目录)是类 Unix 系统中使用最广泛的命令,用于归 ...
- gnome3
http://askubuntu.com/questions/67753/how-do-i-add-an-application-to-the-dash https://wiki.gnome.org/ ...
- Java学习笔记之接口和抽象类
接口(interface)1.interface创建一个接口,implements实现接口 interface jiekou{} class lie implements jiekou{}2.接口可以 ...
- MyBatis-配置缓存
<cache type="org.mybatis.caches.ehcache.LoggingEhcache"> <property name="tim ...
- Drivers Dissatisfaction
Drivers Dissatisfaction time limit per test 4 seconds memory limit per test 256 megabytes input stan ...
- MFC笔记<持续更新>
1.设置垂直滚动条的位置在末尾 SCROLLINFO si; GetScrollInfo(SB_VERT, &si, SIF_PAGE | SIF_RANGE | SIF_POS); si.f ...
- 实测switch支持的参数类型
@Test public void testSwitch() { switch (2) { case 1: System.out.println("int型:" + 1); bre ...
- oracle10g遇到ORA-00257归档程序错误,在释放之前仅限于内部连接
一.简要介绍 首先数据库日志文件有两种: 联机日志文件和归档日志文件,联机日志文件会将之前的覆盖,不会做备份. 而归档日志文件会做备份,这样就造成了归档日志空间已满,解决方法: 如果真的是归档日志空间 ...
- 快学Scala-第二章 控制结构和函数
知识点: 1.条件表达式 if(x>0) 1 else 0 scala每个表达式都有一个类型,如果一致则为它们的类型,如果为混合类型表达式,则为它们的公共超类型Any. if(x>0) 1 ...