最短距离和人流量

最短距离和人流量

题目:

作为一个城市(城市标号从1开始)的应急救援队伍的负责人,你有一张特殊的全国地图。在地图上显示有多个分散的城市和一些连接城市的快速道路。
每条道路都有着距离和人流量 二个指标。人流量越大越容易有堵塞的意外发生。
每次救援的时候,都会给你起点和终点。 要求你输出到达终点的最短距离和对应的人流量和(如果最短距离有多条路线,人流量总和小的优先选择)
输入
输入n,m , n个城市m条道路。 然后m行个 a b c d
表示 a 和b 之间有一条双向边,长度为 c ,人流量为d
最后一行是 s ,e
s为救援起点 ,e为终点
n<1000
输出
输出 一行 有2个数, 最短距离和 人流量总和
样例输入
3 2
1 2 5 6
2 3 4 5
1 3
0 0
样例输出
9 11
不知道Dijkstra 还是先去学一下吧

代码:

#include <stdio.h>
#inc lude <string.h>
#include <algorithm>
#include<iostream>
using namespace std;
const int inf = 8888888;
int n,m;
int map_len[105][105],map_time[105][105];
int vis[105],cost_len[105],cost_time[105];

void Dijkstra(int s,int e)
{
    int i,j,min,pos;
    memset(vis,0,sizeof(vis));
    cost_len[s] = 0;
    cost_time[s] = 0;
    vis[s] = 1;
    for(i = 0; i<n; i++)
    {
        cost_len[i] = map_len[s][i];
        cost_time[i] = map_time[s][i];
    }
    for(i = 1; i<n; i++)
    {
        min = inf;
        for(j = 0; j<n; j++)
        {
            if(cost_len[j]<min && !vis[j])
            {
                pos = j;
                min = cost_len[j];
            }
        }
        vis[pos] = 1;
        for(j = 0; j<n; j++)
        {
            if(cost_len[pos]+map_len[pos][j]<cost_len[j] && !vis[j])
            {
                cost_len[j] = cost_len[pos]+map_len[pos][j];
                cost_time[j] = cost_time[pos]+map_time[pos][j];
            }
            else if(cost_len[pos]+map_len[pos][j]==cost_len[j] && !vis[j])//路程相同的情况下找时间少的
            {
                if(cost_time[pos]+map_time[pos][j]<cost_time[j])
                {
                    cost_time[j] = cost_time[pos]+map_time[pos][j];
                }
            }
        }
    }
}


int main()
{
    int s,e,len,time;
    int i,j;
    scanf("%d%d",&n,&m);

        for(i = 0; i<100; i++)
        {
            for(j = 0; j<100; j++)
                map_len[i][j] = map_time[i][j] = inf;
            map_len[i][i] = map_time[i][i] = 0;
        }
        while(m--)
        {
            scanf("%d%d%d%d",&s,&e,&len,&time);
            s--;
            e--;
            if(len<map_len[s][e])
            {
                map_len[s][e] = map_len[e][s] = len;
                map_time[s][e] = map_time[e][s] = time;
            }
        }

        scanf("%d%d",&s,&e);
        s--;
        e--;
        Dijkstra(s,e);
        printf("%d %d\n",cost_len[e],cost_time[e]);


    return 0;
}

编译样例:

在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值