hdu1874-畅通工程续

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<map>
#include<queue>
#include <deque>
#include <list>
#include <ctime>
#include <stack>
#include <vector>
#include<set>
#define Maxn 256
#define MOD
typedef long long ll;
#define FOR(i,j,n) for(int i=j;i<=n;i++)
#define DFR(i,j,k) for(int i=j;i>=k;--i)
#define lowbit(a) a&-a
#define Max(a,b) a>b?a:b
#define Min(a,b) a>b?b:a
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
using namespace std;
int map_s[Maxn][Maxn],n,m,a,b,d;
int visit[Maxn],dis[Maxn];
int Dijkstra(int a,int b ,int n)
{     int i,j;
      FOR(i,1,n)
      dis[i]=map_s[a][i],visit[i]=false;
      dis[a]=0;
      visit[a]=true;
      FOR(i,1,n)
      { int k,minn=inf;
        FOR(j,1,n)
        if(!visit[j]&&dis[j]<minn) minn=dis[k=j];
        if(minn==inf) break;
        visit[k]=true;
        FOR(j,1,n)
        if(!visit[j]&&dis[j]>dis[k]+map_s[k][j])
        dis[j]=dis[k]+map_s[k][j];
      }
      return dis[b];
}
int main()
{   while(~scanf("%d%d",&n,&m))
    {   FOR(i,1,n)
          FOR(j,1,n)
          if(i==j)  map_s[i][j]=0;
          else map_s[i][j]=inf;
          while(m--)
            {
            scanf("%d%d%d",&a,&b,&d);
            if(map_s[a+1][b+1]>d) map_s[a+1][b+1]=map_s[b+1][a+1]=d;
            }
          scanf("%d%d",&a,&b);

          if(Dijkstra(a+1,b+1,n)<inf) printf("%d\n",Dijkstra(a+1,b+1,n));
          else printf("-1\n");
    }
    return 0;
}




你可能感兴趣的:(hdu1874-畅通工程续)