HDU 3499 Flight(dijkstra+思维)

本文介绍了一道关于寻找最经济飞行路线的算法题,通过Dijkstra算法计算最低成本,并考虑了特殊信用卡优惠条件下的最优解。

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3499点击打开链接

Flight

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 3203    Accepted Submission(s): 670


Problem Description
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?
 

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. 
 

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.
 

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
Hint
In the first sample, Shua Shua should use the card on the flight from Beijing to Chengdu, making the route Harbin->Beijing->Chengdu have the least total cost 800. In the second sample, there's no way for him to get to Chengdu from Harbin, so -1 is needed.
 

Author
Edelweiss

枚举所有边 对每条边的起点计算由起点到该边的起点 以及终点到该边的终点

#include<bits/stdc++.h>
using namespace std;
map<string ,int >mmap;
struct xjy
{
    int be;
    int en;
    long long int dis;
    bool operator < (const xjy &r)const
    {
        return dis>r.dis;
    }
};
vector<xjy> bes[100100],ens[100100];
int n,m;
long long int disbe[100100];
long long int disen[100100];
void bedijkstra(int be)
{
    for(int i=1;i<=n;i++)
        {
            disbe[i]=9223372036854775807;
        }
    priority_queue<xjy > q;
    xjy mid;
    mid.be=be;
    mid.dis=0;
    q.push(mid);
    disbe[be]=0;
    while(!q.empty())
    {
        mid=q.top();
        q.pop();
        for(int i=0;i<bes[mid.be].size();i++)
        {
            if((mid.dis+bes[mid.be][i].dis)<disbe[bes[mid.be][i].en])
            {
                disbe[bes[mid.be][i].en]=mid.dis+bes[mid.be][i].dis;
                xjy midmid;
                midmid.be=bes[mid.be][i].en;
                midmid.dis=disbe[bes[mid.be][i].en];
                q.push(midmid);
            }
        }
    }
}

void endijkstra(int be)
{
    for(int i=1;i<=n;i++)
        {
            disen[i]=9223372036854775807;
        }
    priority_queue<xjy > q;
    xjy mid;
    mid.be=be;
    mid.dis=0;
    q.push(mid);
    disen[be]=0;
    while(!q.empty())
    {
        mid=q.top();
        q.pop();
        for(int i=0;i<ens[mid.be].size();i++)
            {

                if(mid.dis+ens[mid.be][i].dis<disen[ens[mid.be][i].en])
                {
                    disen[ens[mid.be][i].en]=mid.dis+ens[mid.be][i].dis;
                    xjy midmid;
                    midmid.be=ens[mid.be][i].en;
                    midmid.dis=disen[ens[mid.be][i].en];
                    q.push(midmid);
                }

            }

    }
}
int main()
{
    while(cin >> n >> m)
    {
        mmap.clear();
        for(int i=1;i<100100;i++)
            bes[i].clear(),ens[i].clear();
        int cnt=1;
        for(int i=1;i<=m;i++)
        {
            long long int mid;
            string mid1,mid2;
            cin >> mid1>> mid2;
            cin >> mid;
            if(!mmap[mid1])
                mmap[mid1]=cnt++;
            if(!mmap[mid2])
                mmap[mid2]=cnt++;
            xjy midmid;
            midmid.dis=mid;
            midmid.be=mmap[mid1];
            midmid.en=mmap[mid2];
            bes[mmap[mid1]].push_back(midmid);
            midmid.dis=mid;
            midmid.be=mmap[mid2];
            midmid.en=mmap[mid1];
            ens[mmap[mid2]].push_back(midmid);
        }
        string mid1,mid2;
        cin >> mid1 >> mid2;
        if(!mmap[mid1]||!mmap[mid2])
            printf("-1\n");
        else
        {
            bedijkstra(mmap[mid1]);
            endijkstra(mmap[mid2]);
            long long int minn=9223372036854775807;
            for(int i=1;i<=n;i++)
                for(int j=0;j<bes[i].size();j++)
                {
                    minn=min(bes[i][j].dis/2+disbe[bes[i][j].be]+disen[bes[i][j].en],minn);
                }
            cout << minn << endl;
        }
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值