其他分享
首页 > 其他分享> > bellman_ford

bellman_ford

作者:互联网

#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,edge,ff,ll,dis
来源: https://www.cnblogs.com/leyuo/p/16643863.html