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?

InputThere 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. 
OutputOne 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.
这个题的坑点在于建单向边,然后跑两边Dijkstra相当于处理前缀和后缀 然后枚举边就行了,还有初始化INF要大 可能爆longlong
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<map>
#include<cmath>
#define Inf 100000000000
const int maxn=1e5+;
typedef long long ll;
using namespace std;
map<string,int>mp;
struct edge
{
int u,v;
ll w;
int next;
}Edge[*maxn];
struct node
{
int pos;
ll w;
node(int x,int y)
{
pos=x;
w=y;
}
bool friend operator < (node x,node y)
{
return x.w>y.w;
}
};
int head[maxn];
bool vis[maxn];
int cnt;
ll dis[maxn], dis2[maxn];
int u[*maxn],v[*maxn];
ll w[*maxn];
void add(int u,int v,int w)
{
Edge[cnt].u=u;
Edge[cnt].v=v;
Edge[cnt].w=w;
Edge[cnt].next=head[u];
head[u]=cnt++;
}
void Dijkstra(int s)
{
dis[s]=;
priority_queue<node>q;
q.push(node(s,));
while(!q.empty())
{
node now=q.top();
q.pop();
if(vis[now.pos])continue;
vis[now.pos]=;
for(int i=head[now.pos];i!=-;i=Edge[i].next)
{
if(dis[now.pos]+Edge[i].w<dis[Edge[i].v])
{ dis[Edge[i].v]= dis[now.pos]+Edge[i].w;
q.push(node(Edge[i].v,dis[Edge[i].v]));
}
}
}
return ;
}
void Dijkstra1(int s)
{
dis2[s]=;
priority_queue<node>q;
q.push(node(s,));
while(!q.empty())
{
node now=q.top();
q.pop();
if(vis[now.pos])continue;
vis[now.pos]=;
for(int i=head[now.pos];i!=-;i=Edge[i].next)
{
if(dis2[now.pos]+Edge[i].w<dis2[Edge[i].v])
{ dis2[Edge[i].v]= dis2[now.pos]+Edge[i].w;
q.push(node(Edge[i].v,dis2[Edge[i].v]));
}
}
}
return ;
}
int main()
{
int m,n;
while(cin>>n>>m)
{
int cc=;
cnt=;
memset(head,-,sizeof(head));
memset(vis,,sizeof(vis));
for(int t=;t<=;t++)
{
dis2[t]=Inf;
dis[t]=Inf;
}
string st,ed;
string uu,vv;
mp.clear();
for(int t=;t<m;t++)
{
cin>>uu>>vv>>w[t];
if(mp[uu]==)
{
mp[uu]=cc++;
}
if(mp[vv]==)
{
mp[vv]=cc++;
} add(mp[uu],mp[vv],w[t]);
u[t]=mp[uu];
v[t]=mp[vv]; //add(mp[v],mp[u],w);
}
cin>>st>>ed;
if(st==ed)
{
puts("");
continue;
}
if(mp[st]==)
{
mp[st]=cc++;
}
//cout<<mp[st]<<endl;
if(mp[ed]==)
{
mp[ed]=cc++;
}
Dijkstra(mp[st]);
if(dis[mp[ed]]==Inf)
{
puts("-1");
continue;
}
// for(int t=1;t<=cc;t++)
// {
// dis2[t]=Inf;
// }
memset(vis,,sizeof(vis));
memset(head,-,sizeof(head));
cnt=;
for(int t=;t<m;t++)
{
add(v[t],u[t],w[t]);
//add(mp[v],mp[u],w);
}
Dijkstra1(mp[ed]);
ll ans=;
for(int t=;t<cnt;t++)
{
ans=min(ans,dis[Edge[t].v]+dis2[Edge[t].u]+Edge[t].w/);
}
printf("%lld\n",ans);
}
return ;
}

HDU - 3499 -(Dijkstra变形+枚举边)的更多相关文章

  1. NYOJ 1248 海岛争霸(Dijkstra变形——最短路径最大权值)

    题目链接: http://acm.nyist.net/JudgeOnline/problem.php?pid=1248 描述 神秘的海洋,惊险的探险之路,打捞海底宝藏,激烈的海战,海盗劫富等等.加勒比 ...

  2. POJ.1797 Heavy Transportation (Dijkstra变形)

    POJ.1797 Heavy Transportation (Dijkstra变形) 题意分析 给出n个点,m条边的城市网络,其中 x y d 代表由x到y(或由y到x)的公路所能承受的最大重量为d, ...

  3. 【lightoj-1002】Country Roads(dijkstra变形)

    light1002:传送门 [题目大意] n个点m条边,给一个源点,找出源点到其他点的‘最短路’ 定义:找出每条通路中最大的cost,这些最大的cost中找出一个最小的即为‘最短路’,dijkstra ...

  4. hdu 3499 Flight (最短路径)

    Flight Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Su ...

  5. HDU 1688 Sightseeing&HDU 3191 How Many Paths Are There(Dijkstra变形求次短路条数)

    Sightseeing Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  6. hdu 3499 flight 【分层图】+【Dijkstra】

    <题目链接> 题目大意: 现在给你一些点,这些点之间存在一些有向边,每条边都有对应的边权,有一次机会能够使某条边的边权变为原来的1/2,求从起点到终点的最短距离. 解题分析: 分层图最短路 ...

  7. HDU - 3499 Flight 双向SPFA+枚举中间边

    Flight Recently, Shua Shua had a big quarrel with his GF. He is so upset that he decides to take a t ...

  8. HDU 5778 abs (枚举)

    abs 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5778 Description Given a number x, ask positive ...

  9. HDU 2112 HDU Today (Dijkstra算法)

    HDU Today Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

随机推荐

  1. spring data jpa 代码生成!!(精华帖)

    通过数据库动态生成自己想要生成的各种模板,需要了解grovery. view -> Tool Windows -> Database + -> Data source -> M ...

  2. 一个试图了解JVM内存模型的两年经验的初级程序员,透彻!

    所有的编程语言中都有内存模型这个概念,区别于微架构的内存模型,高级语言的内存模型包括了编译器和微架构两部分.我试图了解了Java.C#和Go语言的内存模型,发现内容基本大同小异,只是这些语言在具体实现 ...

  3. Bytom Dapp 开发笔记(一):架构设计

    简介 研究比原链已经一年了,用比原链做了几个dapp,而且最近还做了一个基于他们插件钱包的dapp,总结了一些遇到的坑,还有一些技术细节,接下来我会分成三章,从dapp设计架构上,到深入到源码分析去帮 ...

  4. vue中methods互相调用的方法

    a:function(goods) { this.aa= []; this.bb= 0; this.cc= 0; }, b:function(){ if(this.bbb!= 0){ this.aa= ...

  5. PyCharm 2020.1专业版安装教程及破解方法

    一.安装第一步下载 版本一定得是:2020.1 其他版本下载地址:https://www.jetbrains.com/pycharm/download/other.html  安装不多说了: 二.破解 ...

  6. QT QMdiArea 添加背景或添加背景图片失效问题

    说起QMdirArea 这个控件与其他控件真所不同.... 这里记一下 我踩过的坑之一,,,,, QMdiArea 默认的背景 不符合我要求,,当时我就理所当然就想往常一样给它设置颜色 万万没想到.. ...

  7. 每日一道 LeetCode (14):数组加一

    每天 3 分钟,走上算法的逆袭之路. 前文合集 每日一道 LeetCode 前文合集 代码仓库 GitHub: https://github.com/meteor1993/LeetCode Gitee ...

  8. Spring Boot自定义错误视图

    Spring Boot缺省错误视图解析器 Web应用在处理请求的过程中发生错误是非常常见的情况,SpringBoot中为我们实现了一个错误视图解析器(DefaultErrorViewResolver) ...

  9. 记好这 24 个 ES6 方法,用来解决实际开发的 JS 问题

    本文主要介绍 24 中 es6 方法,这些方法都挺实用的,本本请记好,时不时翻出来看看. 1.如何隐藏所有指定的元素 const hide = (el) => Array.from(el).fo ...

  10. python基础 Day10

    python Day10 函数的参数升级版 形参角度 万能参数*arg #在函数定义时,*代表聚合.他将所有的位置参数聚合成一个元组,赋值给了args def test(*args): print(& ...