HDU2680-Choose the best route(经典最短路问题dijkstra算法)

来源:互联网 发布:centos mysql配置文件 编辑:程序博客网 时间:2024/06/05 06:28

Choose the best route

One day , Kiki wants to visit one of her friends. As she is liable to carsickness , she wants to arrive at her friend’s home as soon as possible . Now give you a map of the city’s traffic route, and the stations which are near Kiki’s home so that she can take. You may suppose Kiki can change the bus at any station. Please find out the least time Kiki needs to spend. To make it easy, if the city have n bus stations ,the stations will been expressed as an integer 1,2,3…n.
Input
There are several test cases.
Each case begins with three integers n, m and s,(n<1000,m<20000,1=

#include<iostream>#include<cstring>#include<cstdio>#include<cmath>#include<algorithm>using namespace std;const int inf =0x3f3f3f3f;const int MAX = 1005;  int n, m, s;  int map[MAX][MAX], dis[MAX], vis[MAX];  void dijkstra(){    memset(vis,0,sizeof(vis));    int i,j,pos,minn;    for(i=1; i<=n; i++)        dis[i]=map[0][i];    vis[0]=1;    for(i=0; i<=n; i++)    {        minn=inf;        for(j=1; j<=n; j++)            if(dis[j]<minn && !vis[j])           {              minn=dis[j];              pos=j;           }        if(minn==inf) break;        vis[pos]=1;        for(j=1; j<=n; j++)        {            if(dis[j]>dis[pos]+map[pos][j] && !vis[j])                dis[j]=dis[pos]+map[pos][j];        }    }}int main(){    while(~scanf("%d%d%d",&n,&m,&s))    {       memset(map,inf,sizeof(map));       for(int i=0; i<=n; i++)          map[i][i]=0;       int p,q,t;       for(int i=0; i<m; i++)       {           scanf("%d%d%d",&p,&q,&t);           map[p][q]=min(map[p][q],t);       }       int w,tmp;       scanf("%d",&w);       for(int i=1; i<=w; i++)        {            scanf("%d",&tmp);            map[0][tmp]=0;        }       dijkstra();       if(dis[s]==inf)          printf("-1\n");       else          printf("%d\n",dis[s]);    }}

方法二:

#include <cstdio>  #include <cstring>  #include <algorithm>  #define inf 0x3f3f3f3f  using namespace std;  const int MAX = 1005;  int n, m, s;  int map[MAX][MAX], dis[MAX], vis[MAX];  void dijkstra(int s)  {       memset(vis, 0, sizeof(vis));     for(int i=1; i<=n; i++)        dis[i] = map[s][i];  //初始化    vis[s] = 1; dis[s] = 0;      int minn,pos;    for(int i=1; i<n; i++)      {          minn=inf;         for(int j = 1; j <= n; j++)              if(!vis[j] && dis[j] < minn)                  minn = dis[pos = j];          if(minn == inf)  break;          vis[pos] = 1;          for(int j=1; j<=n; j++)              if(!vis[j] && dis[pos] + map[pos][j] < dis[j])                  dis[j] = dis[pos] + map[pos][j];      }  }  int main()  {      while(scanf("%d %d %d", &n, &m, &s) != EOF)      {           memset(map, inf, sizeof(map));          while(m--)          {              int x, y, w;              scanf("%d %d %d", &x, &y, &w);              if(w < map[y][x]) map[y][x] = w;     //注意是有向的,反方向           }          dijkstra(s);          int a, b, ans = inf;          scanf("%d", &b);          for(int i=1; i<=b; i++)          {              scanf("%d", &a);              ans = min(ans, dis[a]);  //寻找最小的站点        }          if(ans != inf)printf("%d\n", ans);          else printf("-1\n");      }      return 0;  }  
阅读全文
0 0
原创粉丝点击