因为数据比较小

所以flyod spfa dijkstra 多可以过

Floyd

#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = ; int s[maxn][maxn];
int main (){
int n,m;
while(~scanf("%d %d",&n,&m) ){
for(int i=;i<n;i++){
for(int j=;j<n;j++){
if(i != j)
s[i][j] = INF;
else
s[i][j] = ;
}
}
for(int i=;i<m;i++){
int x,y,v; scanf("%d %d %d",&x,&y,&v);
s[x][y] = min(s[x][y],v);
s[y][x] = min(s[y][x],v);
}
for(int k=;k<n;k++)
for(int i=;i<n;i++)
for(int j=;j<n;j++)
s[i][j] = min(s[i][j],s[i][k]+s[k][j]);
int x,y;
scanf("%d %d",&x,&y);
if(s[x][y] == INF)
printf("-1\n");
else
printf("%d\n",s[x][y]);
}
}

spfa

// Spfa 算法

#include <bits/stdc++.h>
using namespace std;
const int maxn = ;
const int INF = 0x3f3f3f3f; vector <pair<int,int> >E[maxn];
int d[maxn],inq[maxn];//inq 代表的是是否in queue
int n,m;
void init(){
for(int i=;i<maxn;i++) d[i] = INF;
for(int i=;i<maxn;i++) inq[i] = ;
for(int i=;i<maxn;i++) E[i].clear();
}
int main()
{
while (~scanf("%d %d",&n,&m)){
init();
for(int i=;i<=m;i++){
int x,y,v;
scanf("%d %d %d",&x,&y,&v);
E[x].push_back(make_pair(y,v));
E[y].push_back(make_pair(x,v));
}
queue<int>Q;
int s,e;
scanf("%d %d",&s,&e);
d[s] = ;
inq[s] = ;//起点进队列
Q.push( s );
while (Q.size()){
int now = Q.front();
Q.pop(); inq[now] = ;//该点出队列
for(int i=;i < E[now].size();i++){
int y = E[now][i].first;
int v = E[now][i].second;
if(d[y] > d[now] + v )
{
d[y] = d[now] + v;
if( inq[y] )//如果在队列
continue;
inq[y] = ;
Q.push(y);
}
}
}
if(d[e] == INF)
printf("-1\n");
else
printf("%d\n",d[e]); }
return ;
}

dijkstra  //待测试

#include <bits/stdc++.h>
using namespace std;
const int maxn = ;
const int INF = 0x3f3f3f3f;
int n,m;
vector<pair<int,int> > E[maxn];
int d[maxn];
void init(){
for(int i=;i<maxn;i++)
E[i].clear() ,d[i] = INF;
}
int main()
{
while (~scanf("%d %d",&n,&m)){
init();
for(int i=;i<=m;i++){
int x,y,v;
scanf("%d%d%d",&x,&y,&v);
E[x].push_back(make_pair(y,v));
E[y].push_back(make_pair(x,v));
}
int s,e;//start end;
scanf("%d %d",&s,&e);
d[s] = ;
int v = -;
priority_queue<pair<int,int> > Q;
Q.push(make_pair(-d[s],s));
while ( !Q.empty() )
{
int now = Q.top().second;
if( now == e)//相等就可以直接跳出了
break;
Q.pop();
for(int i=;i < E[now].size();i++)
{
v = E[now][i].first;
if(d[v] > d[now] + E[now][i].second )
{
d[v] = d[now] + E[now][i].second;
Q.push(make_pair(-d[v],v));
}
} }
if(d[e] == INF)
printf("-1\n");
else
printf("%d\n",d[e]);
}
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 畅通工程续 Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过 ...

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

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

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

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

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

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

  7. hdu 1874 畅通工程续 Dijkstra

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

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

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

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

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

随机推荐

  1. nodejs(一)process模块

    1.process是一个全局进程,你可以直接通过process变量直接访问它. process实现了EventEmitter接口,exit方法会在当进程退出的时候执行.因为进程退出之后将不再执行事件循 ...

  2. idong常用js总结

    1.判断屏幕高度 $(document).ready(function() {    $("#left").height($(window).height());    $(&qu ...

  3. Unity3d 镜面折射 vertex and frag Shader源代码

    Unity3d 镜面折射  网上能找到的基本上是固定管道或表面渲染的shader. 特此翻译为顶点.片段渲染的Shader, 本源代码仅仅涉及shader与cs部分, 请自行下载NGUI  unity ...

  4. js-jquery-数组遍历

    一.原生方法支持 1.普通for循环 for(j = 0; j < arr.length; j++) { } 说明:性能很高,但是仍可以优化. 2.优化版for循环[推荐] for(j = 0, ...

  5. 网站用sqlite库,报attempt to write a readonly database,解决方法

    将sqlite数据库文件,设置为users完全控制.重启网站即可!

  6. Windows多线程基础

    进程与线程基础 程序: 计算机指令的集合,以文件的形式存储在磁盘上 进程: 正在运行是程序实例,以是一个程序在其自身的地址空间的一次执行活动.进程有一个进程管理的内核对象和地址空间组成. 线程: 程序 ...

  7. 深入浅出TCP之listen

    原文:http://blog.chinaunix.net/uid-29075379-id-3858844.html int listen(int fd, int backlog); 有几个概念需要在开 ...

  8. linux文件系统软链接硬链接

    引子 目前,UNIX的文件系统有很多种实现,例如UFS(基于BSD的UNIX文件系统).ext3.ext4.ZFS和Reiserfs等等. 不论哪一种文件系统,总是需要存储数据.硬盘的最小存储单位是扇 ...

  9. redis error It was not possible to connect to the redis server(s); to create a disconnected multiplexer, disable AbortOnConnectFail. SocketFailure on PING

    应用redis出现如下错误 It was not possible to connect to the redis server(s); to create a disconnected multip ...

  10. 3:3 OGNL 表达式一

    一: 用例 (直接链式访问属性名,其实内部还是的调用set,get方法实现数据的流动); 二: 注意:表达式里面是没有方法的,只能点属性, 访问列表: (访问的时候加上#,表示访问非值栈的内容.) 访 ...