最短路模板题

题目:

在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt。但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线,你可以帮助他们吗?

Input
输入包括多组数据。每组数据第一行是两个整数N、M(N<=100,M<=10000),N表示成都的大街上有几个路口,标号为1的路口是商店所在地,标号为N的路口是赛场所在地,M则表示在成都有几条路。N=M=0表示输入结束。接下来M行,每行包括3个整数A,B,C(1<=A,B<=N,1<=C<=1000),表示在路口A与路口B之间有一条路,我们的工作人员需要C分钟的时间走过这条路。
输入保证至少存在1条商店到赛场的路线。
Output
对于每组输入,输出一行,表示工作人员从商店走到赛场的最短时间
Sample Input
2 1
1 2 3
3 3
1 2 5
2 3 5
3 1 2
0 0
Sample Output
3
2

1.Floyd算法:

#include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
#include <queue>
#include <vector>
#define INF 0x3f3f3f3fusing namespace std;int main()
{int n, m, s, t;while(scanf("%d%d", &n, &m)){if(n==0&&m==0) return 0;vector<vector<int> > dis(n);for(int i = 0; i < n; i++){dis[i].resize(n, INF);dis[i][i] = 0;}for(int i = 0; i < m; i++){int a, b, x;scanf("%d%d%d", &a, &b, &x);a=a-1;b=b-1;if(dis[a][b] > x)dis[a][b] = dis[b][a] = x;}//    scanf("%d%d", &s, &t);for(int k = 0; k < n; k++)for(int i = 0; i < n; i++)for(int j = 0; j < n; j++){if(dis[i][k] < INF && dis[k][j] < INF)dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);}
//        if(dis[s][t] != INF)
//            printf("%d\n", dis[s][t]);
//        else
//            printf("-1\n");printf("%d\n",dis[0][n-1]);}return 0;
}

2.dijkstra算法邻接矩阵实现

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 100100
using namespace std;int vis[1100];int dis[1100];
int map[1100][1100];void dijkstra(int s,int n)//s是起点
{memset(dis, INF, sizeof(dis));for(int i = 1; i <= n; i++)//处理dis为到s的距离{dis[i] = map[s][i];vis[i] = 0;}vis[s] = 1;dis[s] = 0;for(int i = 1; i < n; i++)//执行n-1轮{int min_dis = INF;int x;for(int j = 1; j <= n; j++)//寻找所有集合外的点到集合距离最小的点x{if(!vis[j] && min_dis > dis[j]){x = j;min_dis = dis[j];}}vis[x] = 1;//然后把X加入到最短路点集中for(int j = 1; j <= n; j++)//更新集合外点到集合的距离{if(!vis[j])dis[j] = min(dis[j], dis[x] + map[x][j]);//x到j的距离+dis[x]}}
}int main() {int m,n;int a,b,c;while(cin>>m>>n){if(m==0&&n==0) return 0;memset(vis,0,sizeof(vis));memset(map,INF,sizeof(map));for(int i=0;i<n;++i){scanf("%d%d%d",&a,&b,&c);map[a][b]=map[b][a]=c;}dijkstra(1,m);printf("%d\n",dis[m]);}
} 

3.dijkstra算法队列优化实现

#include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
#include <queue>
#include <vector>
#define INF 0x3f3f3f3fusing namespace std;
const int maxn = 105;
int dis[maxn], pre[maxn];struct Edge//边 
{int u, v, w;Edge() {};Edge(int uu, int vv, int ww): u(uu), v(vv), w(ww) {};
};vector<Edge> edges;//边数组 
vector<int> G[maxn];//存储每个节点对应的边的序号 void init(int nn)//清理 
{for(int i = 0; i <= nn; i++)G[i].clear();edges.clear();
}void AddEdge(int uu, int vv, int ww)//加边 
{edges.push_back(Edge(uu, vv, ww));int edgenum = edges.size();G[uu].push_back(edgenum - 1);
}struct node//优先队列优化,dis小的先出队 
{int u, d;node() {};node(int uu, int dd): u(uu), d(dd) {};friend bool operator < (node a, node b){return a.d > b.d;}
};void dijkstra(int s)
{priority_queue<node> q;memset(dis, INF, sizeof(dis));//dis初始化为INF dis[s] = 0;q.push(node(s, dis[s]));while(!q.empty()){node cur = q.top();q.pop();int from = cur.u;if(cur.d != dis[from])//减少了vis数组,表示该节点被取出来过 continue;for(int i = 0; i < G[from].size(); i++)//更新所有集合外点到集合的dis {Edge e = edges[G[from][i]];if(dis[e.v] > dis[e.u] + e.w){dis[e.v] = dis[e.u] + e.w;pre[e.v] = from;//存储父节点 q.push(node(e.v, dis[e.v]));//将有更新的dis加入到队列中 }}}
}
int main()
{int n, m;while(~scanf("%d%d", &n, &m) && n && m){init(n);for(int i = 0; i < m; i++){int u, v, w;scanf("%d%d%d", &u, &v, &w);AddEdge(u, v, w);AddEdge(v, u, w);}dijkstra(1);printf("%d\n", dis[n]);}return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/560692.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

php 取经纬度,php根据地址获取百度地图经纬度的实例方法

首先我们来看全部实例代码&#xff1a;/*** param string $address 地址* param string $city 城市名* return array*/function getLatLng($address‘‘,$city‘‘){$result array();$ak ‘‘;//您的百度地图ak&#xff0c;可以去百度开发者中心去免费申请$url "http://…

(kruskal算法复习+模板)Eddy's picture

题目&#xff1a; Eddy begins to like painting pictures recently ,he is sure of himself to become a painter.Every day Eddy draws pictures in his small room, and he usually puts out his newest pictures to let his friends appreciate. but the result it can be…

php解密 eval( base64_decode,PHP之eval(gzinflate(base64_decode加密解密

从网上下载了个php版本的小游戏站的源码&#xff0c;一直都没有时间看&#xff0c;今天闲着没事看了下各个文件夹的内容&#xff0c;有一个文件引起了小胡的注意&#xff0c;文件名为php.php&#xff0c;打开之后发现了是加密过后的php文件&#xff0c;于是试着找了下相关的解密…

(kruskal)Jungle Roads

题目 The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extra roads between villages some years ago. But the jungle overtakes roads relentlessly, so the large road network is too expensive to mainta…

php 留言板分页显示,php有分页的留言板,留言成功后怎么返回当前页?

比如我在【index.php?p3】发布留言&#xff0c;成功后怎么返回到 index.php?p3 这个页面&#xff1f;在【index.php?p5】发布留言成功后怎么返回index.php?p5这个页面&#xff1f;location.href"这里该怎么写&#xff1f;"涉及的页面有index.php&#xff0c;doac…

(最短路)HDU Today(hdu2112)

题目 Problem Description 经过锦囊相助&#xff0c;海东集团终于度过了危机&#xff0c;从此&#xff0c;HDU的发展就一直顺风顺水&#xff0c;到了2050年&#xff0c;集团已经相当规模了&#xff0c;据说进入了钱江肉丝经济开发区500强。这时候&#xff0c;XHD夫妇也退居了…

php文件读取文件内容,PHP文件系统函数-读取文件内容几种方式

介绍几种php获取文件内容的方式介绍读取文件的方式之前&#xff0c;我们先看一下打开文件资源和关闭资源名字资源绑定到一个流 - fopen关闭一个已打开的文件指针 - fclose$handle1 fopen("/home/rasmus/file.txt", "r");fclose($hanle1);$handle2 fopen(…

欧几里得算法和扩展欧几里得算法详解

欧几里得算法&#xff1a; int gcd(int x,int y){if(y) return gcd(y,x%y);return x; }扩展欧几里得算法&#xff1a; 先说一个整体思路&#xff1a; 先求AxBygcd(A,B);的一个解x&#xff0c;y 然后我们可以求他的通解 然后求AxByC的通解 我们先看看怎么求AxBygcd(A,B);的一…

php class使用方法,PHP调试类Krumo使用教程

写程序最讨厌的是程序发生错误&#xff0c;但是却又不知道该从何debug起&#xff0c;我们通常会使用print_r 或者 var_dump 或者是 echo 慢慢的debug。如果你跟我一样使用PHP 5开发&#xff0c;现在可以使用Krumo这个简单好用的工具帮助我们做这件事情。虽然IDE也有内建的debug…

(扩展欧几里得)青蛙的约会

题意 两只青蛙在网上相识了&#xff0c;它们聊得很开心&#xff0c;于是觉得很有必要见一面。它们很高兴地发现它们住在同一条纬度线上&#xff0c;于是它们约定各自朝西跳&#xff0c;直到碰面为止。可是它们出发之前忘记了一件很重要的事情&#xff0c;既没有问清楚对方的特…

(待定系数法)A/B

题目&#xff1a; 要求(A/B)%9973&#xff0c;但由于A很大&#xff0c;我们只给出n(nA%9973)(我们给定的A必能被B整除&#xff0c;且gcd(B,9973) 1)。 Input 数据的第一行是一个T&#xff0c;表示有T组数据。 每组数据有两个数n(0 < n < 9973)和B(1 < B < 10^…

matlab 连接数组,matlab数组操作知识点总结

其实如果单从建模来讲&#xff0c;以下大部分函数都用不到&#xff0c;但是这些都是基础。第一点&#xff1a;数组与矩阵概念的区分数组&#xff1a;与其它编程语言一样&#xff0c;定义是&#xff1a;相同数据类型元素的集合。矩阵&#xff1a;在数学中&#xff0c;矩阵(Matri…

(组合数求模=乘法逆元+快速幂) Problem Makes Problem

题目&#xff1a; As I am fond of making easier problems, I discovered a problem. Actually, the problem is ‘how can you make n by adding k non-negative integers?’ I think a small example will make things clear. Suppose n4 and k3. There are 15 solutions.…

php做图书网站,基于PHP的图书馆网站管理系统的设计与实现

《现代图书情报技术 》 年 第 期 工作交流 总第 期基于 的图书馆网站管理系统的设计与实现 郑婷婷 张 羽 辽宁大学图书馆 沈阳 【摘要 】 阐述了在 下 , 使用 十 设计并实现 了图书馆网站管理系统 。 分系统平台如何搭建 、 系统结构如何设计以及最终如何实现三大部分 。 【关键…

(小费马定理降幂)Sum

题目&#xff1a; 分析与解答&#xff1a; 参考思路&#xff1a; https://www.cnblogs.com/stepping/p/7144512.html https://blog.csdn.net/strangedbly/article/details/50996908 根据隔板定理&#xff0c;把N分成一份的分法数为C(1,n-1)&#xff0c; 把N分成两份的分法…

温度 数值模拟 matlab,西安交通大学——温度场数值模拟(matlab)

西安交通大学材料制备与成型实验——温度场数值模拟,matlab编程温度场模拟matlab代码&#xff1a;clear,clc,clfL18;L28;N9;M9;% 边长为8cm的正方形划分为8*8的格子 T0500;Tw100; % 初始和稳态温度 a0.05; % 导温系数tmax600;dt0.2; % 时间限10min和时间步长0.2s dxL1/(M-1);dy…

matlab 参数识别,[转载]自编最小二乘法的Matlab参数辨识程序(含实例)

function [sysd,sys,err] ID(Y,U,Ts)%%基于递推最小二乘法的参数辨识程序%仅针对二阶系统&#xff1a;)%出处&#xff1a;http://blog.sina.com.cn/xianfa110%---------------%Inputs:%---------------%Y nX1 vector of your model output%U nX1 vector of your model input…

(回文串全排列个数) xiaoxin juju needs help

题目 As we all known, xiaoxin is a brilliant coder. He knew palindromic strings when he was only a six grade student at elementry school. This summer he was working at Tencent as an intern. One day his leader came to ask xiaoxin for help. His leader gav…

让apache解析html里的php代码,让Apache解析html文件中的php语句

为什么要干这种事呢&#xff1f;原因在于:对于纯粹的网页来说(不涉及对于数据库的操作)&#xff0c;可以使用一些软件来生成html代码。推荐软件Axure但是&#xff0c;当生成html文件之后&#xff0c;你发现还要写php语句对数据库进行操作时&#xff0c;就会遇到一些问题。首先&…

(找循环节)Number Sequence

题目&#xff1a; A number sequence is defined as follows: f(1) 1, f(2) 1, f(n) (A * f(n - 1) B * f(n - 2)) mod 7. Given A, B, and n, you are to calculate the value of f(n). Input The input consists of multiple test cases. Each test case contains…