最短路径(shopth)

最短路径(shopth)(floyd)

Description
给出一个有向图G=(V,E),和一个源点v0∈V,请写一个程序输出v0和图G中其它顶点的 最短路径。只要所有的有向环权值和都是正的,我们就允许图的边有负值。顶点的标号从 1 到n(n为图G的顶点数)

Input
第 1 行:一个正数 n(2≤n≤80)表示图 G 的顶点总shu

第 2 行:一个整数,表示源点v0(v0∈V,v0可以是图G中任意一个顶点)。

第 3 至第 n+2 行,用一个邻接矩阵 W 给出了这个图。

Output
共包含 n−1 行,按照顶点编号从小到大的顺序,每行输出源点 v0 到一个顶点的最短距 离。每行的具体格式参照样例.

啊啊啊因为有可能因为有负值所以所以!!!他就不能用dijskstra!!

Samples
Input 复制
5
1
0 2 - - 10
- 0 3 - 7
- - 0 4 -
- - - 0 5
- - 6 - 0
Output
(1 -> 2) = 2
(1 -> 3) = 5
(1 -> 4) = 9
(1 -> 5) = 9

Dijkstra:适用于权值为非负的图的单源最短路径,用斐波那契堆的复杂度O(E+VlgV)
BellmanFord:适用于权值有负值的图的单源最短路径,并且能够检测负圈,复杂度O(VE)
SPFA:适用于权值有负值,且没有负圈的图的单源最短路径,论文中的复杂度O(kE),k为每个节点进入Queue的次数,且k一般<=2,但此处的复杂度证明是有问题的,其实SPFA的最坏情况应该是O(VE).
Floyd:每对节点之间的最短路径。
原文链接:https://blog.csdn.net/xiazdong/article/details/8193680

#include <math.h>
#include<bits/stdc++.h> 
#include <algorithm>
#include <iostream>
#include <queue>
#include <cstdio>
#include <list>
#include <deque>
#include <set>
#include <vector>
#include <map>
#include <cstring>
#include <iomanip>
#define max(a,b) (a>b?a:b)
#define pi 3.141592653589793
typedef long long ll;
using namespace std;
//const int maxn=1e5+100;
const int inf=0x3f3f3f3f;
int read() {
	int x=0,f=1;
	char c=getchar();
	while(c<'0'||c>'9') {
		if(c=='-') f=-1;
		c=getchar();
	}
	while(c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
	return x*f;
}
int a[200][200];
int main()
{
    int n,s;
    cin>>n>>s;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            int b;
            if(scanf("%d",&b)==1)//如果b是整数
                a[i][j]=b;
            else
                a[i][j]=inf;
        }
    }
 
    for(int k=1;k<=n;k++)//中间点 
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                if(a[i][j]>a[i][k]+a[k][j])
                    a[i][j]=a[i][k]+a[k][j];
 
    for(int i=1;i<=n;i++){
    	if(i==s) continue; 
        printf("(%d -> %d) = %d\n",s,i,a[s][i]);
	}
        
 
    return 0;
}
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值