博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
dijkstra板子
阅读量:3953 次
发布时间:2019-05-24

本文共 2105 字,大约阅读时间需要 7 分钟。

描述

给定带权无向图G(V,E)和源点s/终点t,求一条 s->t 的最短路径。假设读入边的列表是有(字典)序的(既邻接表就是有序的)。

输入

第一行包含4个整数N、M、s、t,表示该图共有N个结点和M条无向边。(N <= 5000,M <= 200000)。起点为s,终点为t

接下来M行,每行包含3个整数{u,v,w},表示有一条权值为w的无向边连接结点u、v

输出

输出最短路的长度

若无法到达,输出"No path"

样例输入

4 5 1 41 2 21 3 11 4 52 4 33 4 5

样例输出

5
#include
#include
#include
using namespace std;typedef long long ll;typedef pair
PII;struct Node{ int var,next,val;} edge[100000005];int head[100005],tot,dis[100005],N,M;bool vis[100005];priority_queue
Q;void add(int a, int b, int c){ edge[++tot].var = b; edge[tot].val = c; edge[tot].next = head[a]; head[a] = tot;}void init()//多组输入调用{ tot=0; memset(head,0,sizeof(head));}void dijkstra(int s){ memset(dis,0x3f,sizeof(dis)); //memset(vis,0,sizeof(vis)); //while(Q.size()) Q.pop(); dis[s] = 0; Q.push(make_pair(0,s)); while(!Q.empty()) { int x=Q.top().second; Q.pop(); if(vis[x])continue; vis[x]=1; for(int i=head[x]; i; i=edge[i].next) { int y=edge[i].var; if(dis[x]+edge[i].val

用map容器把字符串改换成对应的int型数字, 另外就是要记录下一共出现了多少不同的字符串

#include
#include
#include
#include
#include
using namespace std;const int INF=0x3f3f3f3f;int n,mm,ans;char s1[105],s2[105];bool vis[2000];int tu[105][105];int num[105];int dijkstra(int a,int b) //找两点间的最短路{ memset(vis,0,sizeof(vis)); for(int i=a;i<=b;i++) num[i]=tu[a][i]; num[a]=0; for(int i=a;i<=b;i++) { int min=INF,u=-1; for(int j=a;j<=b;j++) { if(!vis[j]&&num[j]
num[u]+tu[k][u]) num[k]=num[u]+tu[k][u]; } return num[2];}int main(){ map
m; //定义一个map函数 名为mat; ios::sync_with_stdio(false); while(cin>>n&&n>=0) { m.clear(); //清空mat; memset(tu,INF,sizeof(tu)); cin>>s1>>s2; int flag=0; if(!strcmp(s1,s2)) //判断起点和终点是否相等; flag=1; m[s1]=1; //s1用数字1表示; 字符数字化; m[s2]=2; ans=2; //从2开始代替标识; for(int i=0;i
>s1>>s2>>mm; if(!m[s1]) //判断是否与起点相同;不同加以1; 相等时mat[s1]=1;不等=0; m[s1]=++ans; if(!m[s2]) //判断是否与终点相同;不同加以1; m[s2]=++ans; if(mm

 

转载地址:http://ppyzi.baihongyu.com/

你可能感兴趣的文章
【Asp.net】基本概念
查看>>
【Asp.net】Web服务器控件
查看>>
【Asp.net】内置对象
查看>>
C语言数据类型笔记 by STP
查看>>
C语言指针笔记 by STP
查看>>
CoreLocation笔记 by STP
查看>>
Application Transport Security has blocked a cleartext HTTP (http://) 解决方案
查看>>
The identity used to sign the executable is no longer valid.解决方案
查看>>
Xcode增加pch文件
查看>>
CocoaPods安装和使用笔记 by STP
查看>>
Could not find developer disk image-解决方案
查看>>
升级Xcode之后VVDocumenter-Xcode不能用的解决办法
查看>>
iOS开发常见报错及解决方案 by STP
查看>>
SVN(Cornerstone)屏蔽/忽略不需要版本控制的UserInterfaceState.xcuserstate
查看>>
IOS 8 以上版本 设置applicationIconBadgeNumber和消息推送
查看>>
git常用命令
查看>>
Java 基本数据类型笔记by STP
查看>>
IDEA创建Maven项目时 loading archetype list转菊花转十年解决方案
查看>>
Mac启动tomcat
查看>>
报错: java.sql.SQLException: The server time zone value '�й�' is unrecognized or represents more ...
查看>>