hdu 3790 最短路径模版

本文介绍了一种求解给定点集和边集中的最短路径及其最小成本的算法实现,通过Dijkstra算法的改进版来寻找从指定起点到终点的最优路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

给你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<n<=1000, 0<m<100000, s != t)
Output
输出 一行有两个数, 最短距离及其花费。
Sample Input
3 2
1 2 5 6
2 3 4 5
1 3
0 0
Sample Output

9 11

/*
         _...---.._
       ,'          ~~"--..
      /                   ~"-._
     /                         ~-.
    /              .              `-.
    \             -.\                `-.
     \              ~-.                 `-.
  ,-~~\                ~.
 /     \                 `.
.       \                  `.
|        \                   .
|         \                   \
 .         `.                  \
             \                  \
  `           `.                 \
   `           \.                 \
    `           \`.                \
     .           \ -.               \
     `               -.              \
      .           `    -              \  .
      `            \    ~-             \
       `            .     ~.            \
        .            \      -_           \
        `                     -           \
         .            |        ~.          \
         `            |          \          \
          .           |           \          \
          `           |            `.         \
           `          `              \         \
            .          .              `.        `.
            `          :                \         `.
             \         `                 \          `.
              \         .                 `.         `~~-.
               \        :                   `         \   \
                .        .                   \         : `.\
                `        :                    \        |  | .
                 \        .                    \       |  |
                  \       :                     \      `  |  `
                   .                             .      | |_  .
                   `       `.                    `      ` | ~.;
                    \       `.                    .      . .
                     .       `.                   `      ` `
                     `.       `._.                 \      `.\
                      `        <  \                 `.     | .
                       `       `   :                 `     | |
                        `       \                     `    | |
                         `.     |   \                  :  .' |
"Are you crying? "        `     |    \                 `_-'  |
  "It's only the rain."  :    | |   |                   :    ;
"The rain already stopped."`    ; |~-.|                 :    '
  "Devils never cry."       :   \ |                     `   ,
                            `    \`                      :  '
                             :    \`                     `_/
                             `     .\       "For we have none. Our enemy shall fall."
                              `    ` \      "As we apprise. To claim our fate."
                               \    | :     "Now and forever. "
                                \  .'  :    "We'll be together."
                                 :    :    "In love and in hate"
                                 |    .'
                                 |    :     "They will see. We'll fight until eternity. "
                                 |    '     "Come with me.We'll stand and fight together."
                                 |   /      "Through our strength We'll make a better day. "
                                 `_.'       "Tomorrow we shall never surrender."
     sao xin*/
#include <vector>
#include <iostream>
#include <string>
//#include <map>
#include <stack>
#include <cstring>
#include <queue>
#include <list>
#include <cstdio>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <cctype>
#include <sstream>
#include<functional>
#define INF 0xfffffff
#define eps 1e-6
#define LL long long

using namespace std;
const int maxn=1e3+5;
const int maxx=1e6+5;
//const double q = (1 + sqrt(5.0)) / 2.0;   // 黄金分割数
/*
std::hex    <<16进制   cin>>std::hex>>a>>std::hex>>b
cout.setf(ios::uppercase);cout<<std::hex<<c<<endl;
b=b>>1; 除以2 二进制运算

//f[i]=(i-1)*(f[i-1]+f[i-2]);   错排
/ for (int i=1; i<=N; i++)
        for (int j=M; j>=1; j--)
        {
            if (weight[i]<=j)
            {
                f[j]=max(f[j],f[j-weight[i]]+value[i]);
            }
        }
priority_queue<int,vector<int>,greater<int> >que3;//注意“>>”会被认为错误,
priority_queue<int,vector<int>,less<int> >que4;////最大值优先
//str
//tmp
//vis
//val
//cnt     2486   hdu 3790最短路径
*/
int n,m;
int map[1005][1005];
int cost[1005][1005];
void dijkstra(int st,int ed)
{
    int i,j,v,Min;
    int visit[1005],dis[1005],value[1005];
    for(i=1;i<=n;i++)
    {
        dis[i]=map[st][i];
        value[i]=cost[st][i];
    }
    memset(visit,0,sizeof(visit));
    visit[st]=1;
    for(i=1;i<n;i++)
    {
        Min=INF;
        for(j=1;j<=n;j++)
        if(!visit[j]&&dis[j]<Min)
        {
            v=j;
            Min=dis[j];
        }
        visit[v]=1;
        for(j=1;j<=n;j++)
        {
            if(!visit[j]&&map[v][j]<INF)
            {
                if(dis[j]>dis[v]+map[v][j])
                {
                    dis[j]=dis[v]+map[v][j];
                    value[j]=value[v]+cost[v][j];
                }
                else if(dis[j]==dis[v]+map[v][j])
                {
                    if(value[j]>value[v]+cost[v][j])
                    value[j]=value[v]+cost[v][j];
                }
            }
        }
    }
    printf("%d %d\n",dis[ed],value[ed]);
}
int main()
{
    int i,j,st,ed;
    int a,b,c,d;
    while(scanf("%d%d",&n,&m),n||m)
    {
        for(i=1;i<=n;i++)
        for(j=1;j<=n;j++)
        {
            map[i][j]=INF;
            cost[i][j]=INF;
        }
        while(m--)
        {
            scanf("%d%d%d%d",&a,&b,&c,&d);
            if(map[a][b]>c)
            {
                map[a][b]=map[b][a]=c;
                cost[a][b]=cost[b][a]=d;
            }
            else if(map[a][b]==c)
            {
                if(cost[a][b]>d)
                cost[a][b]=cost[b][a]=d;
            }
        }
        scanf("%d%d",&st,&ed);
        dijkstra(st,ed);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值