设为首页 加入收藏

TOP

hdu3790最短路径问题(BFS+优先队列)
2014-11-23 21:12:43 来源: 作者: 【 】 浏览:5
Tags:hdu3790 路径 问题 BFS 优先 队列

Problem Description
给你n个点,m条无向边,每条边都有长度d和花费p,给你起点s终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的。


Input
输入n,m,点的编号是1~n,然后是m行,每行4个数 a,b,d,p,表示a和b之间有一条边,且其长度为d,花费为p。最后一行是两个数 s,t;起点s,终点。n和m为0时输入结束。
(1


Output
输出 一行有两个数, 最短距离及其花费。


Sample Input
3 2
1 2 5 6
2 3 4 5
1 3
0 0

Sample Output
9 11

#include   #include   #include   using namespace std;    typedef struct n1  {      int  x,dist,mony;      friend bool operator<(n1 a,n1 b)      {          if(b.dist>a.dist)          return b.dist=a.mony)          return b.monyp)              map[n1][n2].mony=map[n2][n1].mony=p;          }          else if(map[n1][n2].dist==0||map[n1][n2].dist>d)          {              map[n1][n2].dist=map[n2][n1].dist=d;              map[n1][n2].mony=map[n2][n1].mony=p;          }      }      scanf("%d%d",&s,&t);  }  void BFS(int n)  {      priority_queue Q;      node q,p;      int i;      q.mony=0; q.dist=0;q.x=t;      Q.push(q);      while(!Q.empty())      {          q=Q.top();          Q.pop();          if(q.x==s)          {              min_dist=q.dist;min_mony=q.mony;              break;          }          for(i=1;i<=n;i++)          if(map[q.x][i].dist&&!vist[q.x][i])          {              vist[q.x][i]=vist[i][q.x]=1;//这样就不会走重复的路               p.dist=map[q.x][i].dist+q.dist;              p.mony=map[q.x][i].mony+q.mony;              p.x=i;              Q.push(p);          }      }  }  int main()  {      int n,m;      while(scanf("%d%d",&n,&m)>0&&(n||m))      {          set(n,m);          BFS(n);          printf("%d %d\n",min_dist,min_mony);      }  }  /* 5 7 1 2 5 5 2 3 4 5 1 3 4 6 3 4 2 6 3 5 4 7 4 5 2 6 1 3 4 4 1 5 8 11 5 7 1 2 5 5 2 3 4 5 1 3 4 6 3 4 2 2 3 5 4 7 4 5 2 4 1 3 4 4 1 5 8 10 */  #include
#include
#include
using namespace std;

typedef struct n1
{
    int  x,dist,mony;
    friend bool operator<(n1 a,n1 b)
    {
        if(b.dist>a.dist)
        return b.dist=a.mony)
        return b.monyp)
            map[n1][n2].mony=map[n2][n1].mony=p;
        }
        else if(map[n1][n2].dist==0||map[n1][n2].dist>d)
        {
            map[n1][n2].dist=map[n2][n1].dist=d;
            map[n1][n2].mony=map[n2][n1].mony=p;
        }
    }
    scanf("%d%d",&s,&t);
}
void BFS(int n)
{
    priority_queue Q;
    node q,p;
    int i;
    q.mony=0; q.dist=0;q.x=t;
    Q.push(q);
    while(!Q.empty())
    {
        q=Q.top();
        Q.pop();
        if(q.x==s)
        {
            min_dist=q.dist;min_mony=q.mony;
            break;
        }
        for(i=1;i<=n;i++)
        if(map[q.x][i].dist&&!vist[q.x][i])
        {
            vist[q.x][i]=vist[i][q.x]=1;//这样就不会走重复的路
            p.dist=map[q.x][i].dist+q.dist;
            p.mony=map[q.x][i].mony+q.mony;
            p.x=i;
            Q.push(p);
        }
    }
}
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)>0&&(n||m))
    {
        set(n,m);
        BFS(n);
        printf("%d %d\n",min_dist,min_mony);
    }
}
/*
5 7
1 2 5 5
2 3 4 5
1 3 4 6
3 4 2 6
3 5 4 7
4 5 2 6
1 3 4 4
1 5
8 11
5 7
1 2 5 5
2 3 4 5
1 3 4 6
3 4 2 2
3 5 4 7
4 5 2 4
1 3 4 4
1 5
8 10
*/

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇HDU 4649 Professor Tian(反状态.. 下一篇hdu4337 King Arthur's Knigh..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·我的Linux内核学习笔 (2025-12-26 22:21:10)
·如何评价腾讯开源的 (2025-12-26 22:21:07)
·为什么TCP网络编程中 (2025-12-26 22:21:04)
·Python 数据分析与可 (2025-12-26 21:51:20)
·从零开始学Python之 (2025-12-26 21:51:17)