hdu 2544 

求点1到点n的最短路  无向图

Sample Input
2 1 //结点数 边数
1 2 3 //u v w
3 3
1 2 5
2 3 5
3 1 2
0 0

Sample Output
3
2

堆优化Dijstra模板

 # include <iostream>
# include <cstdio>
# include <cstring>
# include <algorithm>
# include <cmath>
# include <queue>
# define LL long long
using namespace std ; const int INF=0x3f3f3f3f;
const int MAXN=;
struct qnode
{
int v;
int c;
qnode(int _v=,int _c=):v(_v),c(_c){}
bool operator <(const qnode &r)const
{
return c>r.c;
}
};
struct Edge
{
int v,cost;
Edge(int _v=,int _cost=):v(_v),cost(_cost){}
};
vector<Edge>E[MAXN];
bool vis[MAXN];
int dist[MAXN];
int n ;
void Dijkstra(int start)//点的编号从1开始
{
memset(vis,false,sizeof(vis));
for(int i=;i<=n;i++)dist[i]=INF;
priority_queue<qnode>que;
while(!que.empty())que.pop();
dist[start]=;
que.push(qnode(start,));
qnode tmp;
while(!que.empty())
{
tmp=que.top();
que.pop();
int u=tmp.v;
if(vis[u])continue;
vis[u]=true;
for(int i=;i<E[u].size();i++)
{
int v=E[tmp.v][i].v;
int cost=E[u][i].cost;
if(!vis[v]&&dist[v]>dist[u]+cost)
{
dist[v]=dist[u]+cost;
que.push(qnode(v,dist[v]));
}
}
}
}
void addedge(int u,int v,int w)
{
E[u].push_back(Edge(v,w));
} int main ()
{
// freopen("in.txt","r",stdin) ;
int m ;
while (scanf("%d %d" , &n , &m) !=EOF)
{
if (n== && m==)
break ;
int u , v , w ;
int i , j ;
for(i=;i<=n;i++)
E[i].clear(); while(m--)
{
scanf("%d%d%d" , &u , &v , &w) ;
addedge(u,v,w) ;
addedge(v,u,w) ;
}
Dijkstra() ;
printf("%d\n" , dist[n]) ;
} return ;
}

hdu 1874

输出最短需要行走的距离。如果不存在从S到T的路线,就输出-1.

这题有重边

Sample Input
3 3 //结点数 边数
0 1 1//u v w
0 2 3
1 2 1
0 2 //起点 终点
3 1
0 1 1
1 2

Sample Output
2
-1

 # include <iostream>
# include <cstdio>
# include <cstring>
# include <algorithm>
# include <cmath>
# define LL long long
using namespace std ; const int MAXN=;
const int INF=0x3f3f3f3f;
int n ;
bool vis[MAXN];
int cost[MAXN][MAXN] ;
int lowcost[MAXN] ;
int pre[MAXN];
void Dijkstra(int beg)
{
for(int i=;i<n;i++)
{
lowcost[i]=INF;vis[i]=false;pre[i]=-;
}
lowcost[beg]=;
for(int j=;j<n;j++)
{
int k=-;
int Min=INF;
for(int i=;i<n;i++)
if(!vis[i]&&lowcost[i]<Min)
{
Min=lowcost[i];
k=i;
}
if(k==-)
break ;
vis[k]=true;
for(int i=;i<n;i++)
if(!vis[i]&&lowcost[k]+cost[k][i]<lowcost[i])
{
lowcost[i]=lowcost[k]+cost[k][i];
pre[i]=k;
}
} } int main ()
{
// freopen("in.txt","r",stdin) ;
int m ;
while (scanf("%d %d" , &n , &m) !=EOF)
{ int u , v , w ;
int i , j ;
for (i = ; i < n ; i++)
for (j = ; j < n ; j++)
cost[i][j] = INF ;
while(m--)
{
scanf("%d%d%d" , &u , &v , &w) ;
if (w < cost[u][v]) //防止重边
{
cost[u][v] = w ;
cost[v][u] = w ;
}
}
scanf("%d %d" , &u , &v) ;
Dijkstra(u) ;
if (lowcost[v] != INF)
printf("%d\n" , lowcost[v]) ;
else
printf("-1\n") ;
} return ;
}

poj 2387

有重边  无向图

Sample Input

5 5 // m n
1 2 20 //u v w
2 3 30
3 4 20
4 5 20
1 5 100
Sample Output

90

 # include <iostream>
# include <cstdio>
# include <cstring>
# include <algorithm>
# include <cmath>
# define LL long long
using namespace std ; const int MAXN=;
const int INF=0x3f3f3f3f;
int n ;
bool vis[MAXN];
int cost[MAXN][MAXN] ;
int lowcost[MAXN] ;
int pre[MAXN];
void Dijkstra(int beg)
{
for(int i=;i<n;i++)
{
lowcost[i]=INF;vis[i]=false;pre[i]=-;
}
lowcost[beg]=;
for(int j=;j<n;j++)
{
int k=-;
int Min=INF;
for(int i=;i<n;i++)
if(!vis[i]&&lowcost[i]<Min)
{
Min=lowcost[i];
k=i;
}
if(k==-)
break ;
vis[k]=true;
for(int i=;i<n;i++)
if(!vis[i]&&lowcost[k]+cost[k][i]<lowcost[i])
{
lowcost[i]=lowcost[k]+cost[k][i];
pre[i]=k;
}
} } int main ()
{
// freopen("in.txt","r",stdin) ;
int m ;
while (scanf("%d %d" , &m , &n) !=EOF)
{ int u , v , w ;
int i , j ;
for (i = ; i < n ; i++)
for (j = ; j < n ; j++)
{
if (i == j)
cost[i][j] = ;
else
cost[i][j] = INF ;
}
while(m--)
{
scanf("%d%d%d" , &u , &v , &w) ;
if (w < cost[u-][v-]) //防止重边
{
cost[u-][v-] = w ;
cost[v-][u-] = w ;
}
}
Dijkstra(n-) ;
if (lowcost[] != INF)
printf("%d\n" , lowcost[]) ; } return ;
}

hdu 2544 hdu 1874 poj 2387 Dijkstra 模板题的更多相关文章

  1. HDU 2544 最短路 【Dijkstra模板题】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2544 思路:最短路的模板题 Dijkstra 算法是一种类似于贪心的算法,步骤如下: 1.当到一个点时, ...

  2. HDU-2544 最短路 Dijkstra模板题

    题目链接:https://vjudge.net/problem/HDU-2544 题意: 题目要求找到节点1到节点n之间的一条最短路 分析: Dijkstra模板题 单源最短路径,可以用dijkstr ...

  3. HDU 4280:Island Transport(ISAP模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=4280 题意:在最西边的点走到最东边的点最大容量. 思路:ISAP模板题,Dinic过不了. #include & ...

  4. UESTC 30 &&HDU 2544最短路【Floyd求解裸题】

    最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  5. HDU 4347 - The Closest M Points - [KDTree模板题]

    本文参考: https://www.cnblogs.com/GerynOhenz/p/8727415.html kuangbin的ACM模板(新) 题目链接:http://acm.hdu.edu.cn ...

  6. HDU 2089 不要62(数位dp模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=2089 题意:求区间内不包含4和连续62的数的个数. 思路: 简单的数位dp模板题.给大家推荐一个好的讲解博客.h ...

  7. HDU 2222 Keywords Search(AC自动机模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:给出多个单词,最后再给出一个模式串,求在该模式串中包含了多少个单词. 思路: AC自动机的模板题. ...

  8. HDU 1402 A * B Problem Plus (FFT模板题)

    FFT模板题,求A*B. 用次FFT模板需要注意的是,N应为2的幂次,不然二进制平摊反转置换会出现死循环. 取出结果值时注意精度,要加上eps才能A. #include <cstdio> ...

  9. hdu 2586 How far away?(LCA模板题+离线tarjan算法)

    How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

随机推荐

  1. POJ - 3267 The Cow Lexicon(动态规划)

    https://vjudge.net/problem/POJ-3267 题意 给一个长度为L的字符串,以及有W个单词的词典.问最少需要从主串中删除几个字母,使其可以由词典的单词组成. 分析 状态设置很 ...

  2. 使用paramiko远程执行命令、下发文件

    写部署脚本时,难免涉及到一些远程执行命令或者传输文件. 之前一直使用sh库,调用sh.ssh远程执行一些命令,sh.scp传输文件,但是实际使用中还是比较麻烦的,光是模拟用户登陆这一点,还需要单独定义 ...

  3. FastReport.Net报表故障排除方法

    有不少开发人员在使用fastreport报表时遇到过这样的问题,报表设计器工作时,一些工具栏或者工具窗口被损坏了.此时,你应该删除配置文件,该文件是在你启动fastreport时创建的.它位于以下文件 ...

  4. Python中crypto模块进行AES加密和解密

    #coding: utf8 import sys from Crypto.Cipher import AES from binascii import b2a_hex, a2b_hex class p ...

  5. c#将前端传来的Json解析成对象

    描述:因工作中需要将C#中的Json字符串转换为对象,对此记录下. 解决办法: 1.前端传过来的Json字符串,OrderAppModuleJson即前端传递到后端的Json字符串 string st ...

  6. 2018-2019-2 网络对抗技术 20165227 Exp4 恶意代码分析

    2018-2019-2 网络对抗技术 20165227 Exp4 恶意代码分析 实验步骤: 使用的设备:Win7(虚拟机).kali(虚拟机) 实验一:使用如计划任务,每隔一分钟记录自己的电脑有哪些程 ...

  7. Qt 窗体使用 label 标签插入静态图片

    最近在做毕业设计,上位机软件用的Qt,界面当中需要加入学校校徽,结果百度了n多种方法,有用QPixmap的: QPixmap myPix("./school.jpg"); ui-& ...

  8. Debian 安装配置(包括kdevelop)

    最近几天折腾了一下Debian 7 (gnome桌面DVD版,KDE桌面CD版最后会提到),总的来说收获还是挺大的,对比以前使用ubuntu,debian 7给我的感觉像是一个新生婴儿,不带多余的花俏 ...

  9. Linux系统7z文件解压

    获取p7zip_16.02_src_all.tar.bz2 1.解压 tar jxvf p7zip_16.02_src_all.tar.bz2 2.编译 cd p7zip_16.02 make &am ...

  10. Linux的capability深入分析(1)【转】

    转自:https://blog.csdn.net/wangpengqi/article/details/9821227 一)概述: )从2.1版开始,Linux内核有了能力(capability)的概 ...