1450. Russian Pipelines(spfa)

1450

水题 最长路

 1 #include <iostream>

 2 #include<cstdio>

 3 #include<cstring>

 4 #include<algorithm>

 5 #include<stdlib.h>

 6 #include<queue>

 7 #include<vector>

 8 using namespace std;

 9 int n,m;

10 vector<int>ed[510];

11 int vis[510],dis[510],w[510][510];

12 void spfa(int s,int e)

13 {

14     int i;

15     queue<int>q;

16     q.push(s);

17     vis[s] = 1;

18     while(!q.empty())

19     {

20         int u = q.front();

21         q.pop();

22         vis[u] = 0;

23         for(i = 0 ; i < (int)ed[u].size() ; i++)

24         {

25             int v = ed[u][i];

26             if(dis[v]<dis[u]+w[u][v])

27             {

28                 dis[v] = dis[u]+w[u][v];

29                 if(!vis[v])

30                 {

31                     vis[v] = 1;

32                     q.push(v);

33                 }

34             }

35         }

36     }

37     if(dis[e])

38     printf("%d\n",dis[e]);

39     else

40     printf("No solution\n");

41 }

42 int main()

43 {

44     int i;

45     scanf("%d%d",&n,&m);

46     for(i = 1; i <= m; i++)

47     {

48         int u,v,c;

49         scanf("%d%d%d",&u,&v,&c);

50         w[u][v] = c;

51         ed[u].push_back(v);

52     }

53     int s,e;

54     scanf("%d%d",&s,&e);

55     spfa(s,e);

56     return 0;

57 }
View Code

 

你可能感兴趣的:(pipeline)