Spfa(最短路求解)
spfa(最短路求解)
模板:
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
const int maxn = 105;
const int maxm = 100005;
struct Edge{
int to,w,next;
}edge[maxm<<1];
int tot = 0,dis[maxn],cnt[maxn],head[maxn];
void addedge(int u,int v,int w)
{
edge[tot].to = v;edge[tot].w = w;edge[tot].next = head[u];
head[u] = tot++;
}
void spfa(int st,int ed)
{
int i;
queue<int>que;
while (!que.empty()) que.pop();
memset(dis,0x3f3f3f3f,sizeof(dis));
memset(cnt,0,sizeof(cnt));
que.push(st);
dis[st] = 0;
cnt[st] = 1;
while (!que.empty())
{
int u = que.front();
que.pop();
if (u == ed) continue;
for (i = head[u];i != -1;i = edge[i].next)
{
int v = edge[i].to;
if (dis[u] + edge[i].w < dis[v])
{
dis[v] = dis[u] + edge[i].w;
if (!cnt[v]) que.push(v);
cnt[v] = cnt[u];
}
else if (dis[u] + edge[i].w == dis[v])
{
if (!cnt[v]) que.push(v);
cnt[v] += cnt[u];
}
}
}
}
int main()
{
int N,M,S,E,u,v,w,i;
memset(head,-1,sizeof(head));
scanf("%d%d%d%d",&N,&M,&S,&E);
while (M--)
{
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w);
addedge(v,u,w);
}
spfa(S,E);
printf("%d %d\n",dis[E],cnt[E]);
return 0;
}
也可以计算有多少个最小生成树
Spfa(最短路求解)的更多相关文章
- Dijkstra(最短路求解)
Dijkstra(最短路求解) 模板: #include<iostream> #include<cstdio> #include<cstring> #include ...
- NOIP2013 华容道 (棋盘建图+spfa最短路)
#include <cstdio> #include <algorithm> #include <cstring> #include <queue> # ...
- BZOJ 1003 物流运输 (动态规划 SPFA 最短路)
1003: [ZJOI2006]物流运输 Time Limit: 10 Sec Memory Limit: 162 MB Submit: 5590 Solved: 2293 [Submit][Stat ...
- poj1502 spfa最短路
//Accepted 320 KB 16 ms //有n个顶点,边权用A表示 //给出下三角矩阵,求从一号顶点出发到各点的最短路的最大值 #include <cstdio> #includ ...
- hiho(1081),SPFA最短路,(非主流写法)
题目链接:http://hihocoder.com/problemset/problem/1081 SPFA求最短路,是不应-羁绊大神教我的,附上头像. 我第一次写SPFA,我用的vector存邻接表 ...
- hdu 5545 The Battle of Guandu spfa最短路
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5545 题意:有N个村庄, M 个战场: $ 1 <=N,M <= 10^5 $; 其中曹 ...
- HNU 13375 Flowery Trails (spfa最短路)
求最短路径覆盖的全部边权值和. 思路:分别从起点和终点两次求最短路,再比较两个点到起点的距离和他们之间的权值相加和是否等于最短路径. 这题很好 #include <cstring> #in ...
- poj 1847 Tram【spfa最短路】
Tram Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12005 Accepted: 4365 Description ...
- POJ - 1062(昂贵的聘礼)(有限制的spfa最短路)
题意:...中文题... 昂贵的聘礼 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 54350 Accepted: 16 ...
随机推荐
- 一个在linux环境执行io操作的bug
今天项目有了一个奇葩的要求...是什么呢 后台上传了视频后,解析其中的时长,和预览图,并拼接在一起,然而,之东西并不是太麻烦,很快写好了,在本地测试后也没有问题,嗯,发布到测试环境后,一个jar包报错 ...
- JavaScript 作用域(Scope)详解
先对需要用到的名词解释一下,再通过例子深入理解 一.什么是作用域(Scope) [[scope]]:每个javascript函数都是一个对象,对象中有些属性我们可以访问,但有些不可以,这些属性仅供ja ...
- IE浏览器版本的判断
var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串 && userAgent.indexOf(; //判断是否IE< ...
- MyEclipse中快速查看错误
当代码中有错误的时候,MyEclipse会用红线标示错误.这个时候在错误地方按下F2就会显示错误详情了.
- 撩课-Web大前端每天5道面试题-Day20
1.vue生命周期的作用是什么? 它的生命周期中有多个事件钩子,让我们在控制整个Vue实例的过程时更容易形成好的逻辑. 2. Vue实现数据双向绑定的原理:Object.defineProperty( ...
- Mybatis源码正确打开方式
精心挑选要阅读的源码项目: 饮水思源——官方文档,先看文档再看源码: 下载源码,安装到本地,保证能编译运行: 从宏观到微观,从整体到细节: 找到入口,抓主放次,梳理核心流程: 源码调试,找到核心数据结 ...
- spring boot 报错 Failed to read HTTP message
2008-12-13 15:06:03,930 WARN (DefaultHandlerExceptionResolver.java:384)- Failed to read HTTP message ...
- POJ 2773 Happy 2006------欧几里得 or 欧拉函数。
Happy 2006 Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 8359 Accepted: 2737 Descri ...
- C#跨窗体传值
果然C#的跨窗体传值比vb难得多,vb就定义一个全局变量就ok,但是C#还要考虑到命名空间的问题 frmMain要调用LoginUI的两个值,但是在此同时,frmMain又要引用LoginUI,所以说 ...
- python数据类型之简单数据类型
变量使用注意事项 慎用小写字母l和大写字母O,因为它们可能被人看成数值1和0. 应使用小写的python变量名. 字符串 在python中,用引号括起来的都是字符串,其中的引号可以是单引号和双引号. ...