Flight HDU - 3499 (分层最短路)
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
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.
样例一中: Harbin->Beijing->Chengdu ,第二条路线使用半价
题意:
  题面中有中文题意  
 思路:
   是这题的低配版本https://www.cnblogs.com/qieqiemin/p/11298508.html
这种都是套路题,在最短路dis的基础上多开一维维护使用了几次优惠的信息即可,转移就像dp一样。
细节见代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <bits/stdc++.h>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2)ans = ans * a % MOD; a = a * a % MOD; b /= 2;} return ans;}
inline void getInt(int* p);
const int maxn = 100010;
const ll inf = 1e18;
/*** TEMPLATE CODE * * STARTS HERE ***/
struct node
{
    int to;
    int kk;
    ll val;
    node(){}
    node(int tt,int ww,ll vv)
    {
        kk=ww;
        to=tt;
        val=vv;
    }
    bool operator < (const node & b) const
    {
        return val>b.val;
    }
};
std::vector<node> e[maxn];
ll dis[maxn][15];
unordered_map<string,int> info;
void addedge(int a,int b,ll v)
{
    e[a].push_back(node(b,0,v));
}
void init(int n)
{
    for(int i=1;i<=n;++i)
    {
        for(int j=0;j<=2;j++)
            dis[i][j]=inf;
    }
}
priority_queue<node> heap;
int n;
int m,k=1;
void dijkstra(int strat)
{
    init(n);
    dis[strat][0]=0ll;
    heap.push(node(strat,0,0ll));
    node temp;
    while(!heap.empty())
    {
        temp=heap.top();
        heap.pop();
        int now=temp.to;
        ll val=temp.val;
        int kk=temp.kk;
        if(kk>k)
        {
            continue;
        }
        if(val>dis[now][kk])
            continue;
        for(auto x:e[now])
        {
            if(dis[x.to][kk]>val+x.val)
            {
                dis[x.to][kk]=val+x.val;
                heap.push(node(x.to,kk,dis[x.to][temp.kk]));
            }
            if(kk<k&&dis[x.to][temp.kk+1]>val+x.val/2)
            {
                dis[x.to][kk+1]=val+x.val/2;
                heap.push(node(x.to,kk+1,dis[x.to][temp.kk+1]));
            }
        }
    }
}
int main()
{
    //freopen("D:\\code\\text\\input.txt","r",stdin);
    //freopen("D:\\code\\text\\output.txt","w",stdout);
    gbtb;
    while(cin>>n>>m)
    {
        info.clear();
        repd(i,1,n)
        {
            e[i].clear();
        }
        int u,v;ll c;
        string str1,str2;
        int cnt=0;
        while(m--)
        {
            cin>>str1>>str2>>c;
            if(info.count(str1)==0)
            {
                info[str1]=++cnt;
            }
            if(info.count(str2)==0)
            {
                info[str2]=++cnt;
            }
            u=info[str1];
            v=info[str2];
            addedge(u,v,c);
        }
        int s,t;
        cin>>str1>>str2;
        if(info.count(str1)==0)
        {
            info[str1]=++cnt;
        }
        if(info.count(str2)==0)
        {
            info[str2]=++cnt;
        }
        s=info[str1];
        t=info[str2];
        dijkstra(s);
        ll ans=inf;
        repd(i,0,1)
        {
            ans=min(ans,dis[t][i]);
        }
        if(ans==inf)
            ans=-1;
        cout<<ans<<endl;
    }
    return 0;
}
inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch == ' ' || ch == '\n');
    if (ch == '-') {
        *p = -(getchar() - '0');
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 - ch + '0';
        }
    }
    else {
        *p = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 + ch - '0';
        }
    }
}
												
											Flight HDU - 3499 (分层最短路)的更多相关文章
- HDU 3499【最短路】
		
题意: 给你一幅图,然后起点终点,然后有一个条件是可以使某条边的花费减半,求最短路的最小花费. 思路: (来自大哥) 最短路的时候多一维,途中是否有花费减半的边: 然后转移,如果上一条有减半的,这一条 ...
 - 拯救大兵瑞恩 HDU - 4845(状压bfs || 分层最短路)
		
1.状压bfs 这个状压体现在key上 我i们用把key状压一下 就能记录到一个点时 已经拥有的key的种类 ban[x1][y1][x2][y1]记录两个点之间的状态 是门 还是墙 还是啥都没有 ...
 - hdu 4568 Hunter 最短路+dp
		
Hunter Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
 - ROADS POJ - 1724(分层最短路)
		
就是在最短路的基础上 多加了一个时间的限制 , 多一个限制多一维就好了 记住 分层最短路要用dijistra !!! #include <iostream> #include < ...
 - ACM-ICPC 2018 南京赛区网络预赛 L && BZOJ 2763  分层最短路
		
https://nanti.jisuanke.com/t/31001 题意 可以把k条边的权值变为0,求s到t的最短路 解析 分层最短路 我们建立k+1层图 层与层之间边权为0,i 向 i+1层转 ...
 - 分层最短路(牛客第四场)--	free
		
题意: 给你边权,起点和终点,有k次机会把某条路变为0,问你最短路是多长. 思路: 分层最短路模板题.题目有点坑(卡掉了SPFA,只能用dijkstra跑的算法). #include<iostr ...
 - 牛客练习赛47 D	DongDong坐飞机 (分层最短路)
		
链接:https://ac.nowcoder.com/acm/contest/904/D 来源:牛客网 DongDong坐飞机 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 5242 ...
 - hdu 3499 flight 【分层图】+【Dijkstra】
		
<题目链接> 题目大意: 现在给你一些点,这些点之间存在一些有向边,每条边都有对应的边权,有一次机会能够使某条边的边权变为原来的1/2,求从起点到终点的最短距离. 解题分析: 分层图最短路 ...
 - HDU 3499 Flight spfa+dp
		
Flight Time Limit : 20000/10000ms (Java/Other) Memory Limit : 65535/65535K (Java/Other) Total Subm ...
 
随机推荐
- 国内npm源
			
永久使用lnpm config set registry https://registry.npm.taobao.org // 配置后可通过下面方式来验证是否成功 npm config get reg ...
 - JavaScript(1)——编程真善美
			
编程真善美 命名风格: 驼峰命名法 小驼峰法 变量一般用小驼峰法标识.驼峰法的意思是:除第一个单词之外,其他单词首字母大写:camelCase 大驼峰法(即帕斯卡命名法) 相比小驼峰法,大驼峰法把第一 ...
 - Matlab中psf2otf()函数在opencv中的实现
			
在Matlab中有个psf2otf()函数,可以将小尺寸的点扩散函数,扩大尺寸,并作二维傅里叶变换,opencv中没有这个函数,所以编了这么个函数: /************************ ...
 - mariadb数据库集群
			
1.主从架构: 每个从节点需要一个dump线程连接主节点 异步:效率高,安全性低,有延迟 同步:效率低,安全性高,无延迟 主:可读可写,(dump thread) 从:可读不可写 (sql threa ...
 - logistics多分类
			
multiclassification #DATASET: https://archive.ics.uci.edu/ml/datasets/Glass+Identificationimport num ...
 - python AI换脸 用普氏分析法(Procrustes Analysis)实现人脸对齐
			
1.图片效果 2.原代码 # !/usr/bin/python # -*- coding: utf-8 -*- # Copyright (c) 2015 Matthew Earl # # Permis ...
 - select poll epoll之间的区别
			
1.select poll每次循环调用时都需要将文件描述符和事件拷贝到内核空间,epoll只需要拷贝一次: (这种情况在对于描述符数量不大的情况下还可以,但是当描述符的数量达到十几万甚至上百万的时候, ...
 - [转帖]AMD霄龙安全加密虚拟化曝漏洞:已修复
			
AMD霄龙安全加密虚拟化曝漏洞:已修复 https://www.cnbeta.com/articles/tech/862611.htm 硬件的安全问题 今年初,Google的一位研究人员发现,AMD ...
 - Mybatis数据基本操作
			
<insert id="doCreate" parameterType="News"><!--添加数据--> INSERT INTO n ...
 - 树状数组+二维前缀和(A.The beautiful values of the palace)--The Preliminary Contest for ICPC Asia Nanjing 2019
			
题意: 给你螺旋型的矩阵,告诉你那几个点有值,问你某一个矩阵区间的和是多少. 思路: 以后记住:二维前缀和sort+树状数组就行了!!!. #define IOS ios_base::sync_wit ...