C - Flight 两重最短路

题目

Recently, Shua Shua had a big quarrel with his GF. He is so upset that he decides to take a trip to some other city to avoid meeting her. He will travel only by air and he can go to any city if there exists a flight and it can help him reduce the total cost to the destination. There's a problem here: Shua Shua has a special credit card which can reduce half the price of a ticket ( i.e. 100 becomes 50, 99 becomes 49. The original and reduced price are both integers. ). But he can only use it once. He has no idea which flight he should choose to use the card to make the total cost least. Can you help him?

毕业了,乐乐打算去去旅行。目标城市已经定下来了,坐飞机前往。对于高中毕业生,航空公司有一项优惠政策,可以选一条线路半价,100变50,99变49。请你帮他设计一个方案,使得总花费最少。

 Input

There are no more than 10 test cases. Subsequent test cases are separated by a blank line.
The first line of each test case contains two integers N and M ( 2 <= N <= 100,000

0 <= M <= 500,000 ), representing the number of cities and flights. Each of the following M lines contains "X Y D" representing a flight from city X to city Y with ticket price D ( 1 <= D <= 100,000 ). Notice that not all of the cities will appear in the list! The last line contains "S E" representing the start and end city. X, Y, S, E are all strings consisting of at most 10 alphanumeric characters.
有多组测试数据 第一行两个整数n和m,表示n个城市和m条路线 接下来m行,每行表示一条路径,从x到y,花费s元 最后一行两个字符串,表示乐乐从发的起点和目标城市。

 Output

One line for each test case the least money Shua Shua have to pay. If it's impossible for him to finish the trip, just output -1. 输出最少的花费,如果不能到达输出-1

 Sample Input

4 4
Harbin Beijing 500
Harbin Shanghai 1000
Beijing Chengdu 600
Shanghai Chengdu 400
Harbin Chengdu

4 0
Harbin Chengdu

 Sample Output

800
-1

acwing ac 代码  vjudge TLE 

#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#define INF 0x3f3f3f3f
using namespace std;
const int N = 1e5 + 10, M = 5e5 + 10;
struct Edge
{
    int a, b;
    int w;
} edges[M];
int sd[N], ld[N];
int n, m;
void bellman_fordst(int st)
{
    memset(sd, 0x3f, sizeof sd);
    sd[st] = 0;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            int a = edges[j].a, b = edges[j].b, w = edges[j].w;
            if (sd[b] > sd[a] + w)
                sd[b] = sd[a] + w;
        }
    }
}
void bellman_fordlt(int lt)
{
    memset(ld, 0x3f, sizeof ld);
    ld[lt] = 0;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            int a = edges[j].a, b = edges[j].b, w = edges[j].w;
            if (ld[b] > ld[a] + w)
                ld[b] = ld[a] + w;
        }
    }
}
int main()
{
    while (~scanf("%d%d", &n, &m))
    {
        map<string, int> nums;
        string a, b;
        long long c, ans = 1;
        for (int i = 0; i < m; i++)
        {
            cin >> a >> b >> c;
            if (!nums[a])
            {
                nums[a] = ans++;
            }
            if (!nums[b])
            {
                nums[b] = ans++;
            }
            edges[i] = {nums[a], nums[b], c};
        }
        cin >> a >> b;
        if (!nums[a])
        {
            nums[a] = ans++;
        }
        if (!nums[b])
        {
            nums[b] = ans++;
        }
        int st = nums[a], lt = nums[b];
        bellman_fordst(st);
        bellman_fordlt(lt);
        int res = INF;
        for (int j = 0; j < m; j++)
        {
            int a = edges[j].a, b = edges[j].b, w = edges[j].w;
            res = min(res, sd[a] + w / 2 + ld[b]);
        }
        if (sd[lt] == INF)
        {
            puts("-1");
        }
        else
        {
            printf("%d\n", res);
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值