首页 > 其他分享 >bellman_ford

bellman_ford

时间:2022-08-31 17:35:13浏览次数:51  
标签:int tt bellman ford ff ll dis

#include<bits/stdc++.h>
using namespace std;
const int N = 505, M = 100010, INF = 0x3f3f3f3f;
int n, m, dis[N], backup[N], k;

struct edge
{
    int f, t, l;
    edge(){}
    edge(int ff, int tt, int ll)
    {
        f = ff, t = tt, l = ll;
    }
}e[M];

void bellman_ford()
{
    memset(dis, 0x3f, sizeof dis);
    dis[1] = 0;
    for(int i = 0; i < k; i ++ )
    {
        memcpy(backup, dis, sizeof dis);
        for(int j = 0; j < m; j ++ )
        {
            int ff = e[j].f, tt = e[j].t, ll = e[j].l;
            dis[tt] = min(dis[tt], backup[ff] + ll);
        }
    }
}

int main()
{
    cin >>n >> m >> k;
    for(int i = 0; i < m; i ++ )
    {
        int ff, tt, ll;
        cin >> ff >> tt >> ll;
        e[i] = edge(ff, tt, ll);
    }

    bellman_ford();

    if(dis[n] < (INF / 2)) cout << dis[n] << endl;
    else puts("impossible");


    return 0;
}

 

标签:int,tt,bellman,ford,ff,ll,dis
From: https://www.cnblogs.com/leyuo/p/16643863.html

相关文章

  • Bellman-Ford(贝尔曼—福特)
    Bellman-Ford(贝尔曼—福特)时间复杂度O(nm)#include<bits/stdc++.h>usingnamespacestd;#definelllonglong#defineendl"\n"#definesfscanf#definepfprin......