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. 洛谷3月月赛div2 题解(模拟+数学+贪心+数学)

    由于本人太蒻了,div1的没有参加,胡乱写了写div2的代码就赶过来了. T1 苏联人 题目背景 题目名称是吸引你点进来的. 这是一道正常的题,和苏联没有任何关系. 题目描述 你在打 EE Round ...

  2. springboot 使用DruidDataSource 数据源

    一.添加依赖 <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</art ...

  3. cocos2d-x_下载游戏引擎并创建第一个项目

    我是一名小白. 下载并创建游戏项目 第一步:去官网下载cocos2d-x http://www.cocos.com/download 第二步:将安装包里边的 setup.py 拖进命令行点击回车键 , ...

  4. SQL关联查询

    从2张或多张表中,取出有关联的数据 关联查询一共有几种情况: 内连接:INNER JOIN .CROSS JOIN (1)形式一 select 字段列表 from A表 inner join B表 o ...

  5. 小白学习Python之路---py文件转换成exe可执行文件

    一.背景 今天闲着无事,写了一个小小的Python脚本程序,然后给同学炫耀的时候,发现每次都得拉着其他人过来看着自己的电脑屏幕,感觉不是很爽,然后我想着网上肯定有关于Python脚本转换成可执行文件的 ...

  6. Ubuntu18.04安装Nautilus-actions自定义文件管理器鼠标右键列表

    sudo add-apt-repository ppa:daniel-marynicz/filemanager-actions #需要添加源 sudo apt-get install filemana ...

  7. 可以用命令行控制eclipse断点增加删除、远程调试创建与启动的插件

    java # 创建断点(支持条件断点) curl -X PUT -H "Content-Type:application/json" --data '{"language ...

  8. 深度优先搜索(dfs)与出题感想

    在3月23号的广度优先搜索(bfs)博客里,我有提到写一篇深搜博客,今天来把这个坑填上. 第一部分:深度优先搜索(dfs) 以上来自百度百科. 简单来说,深度优先搜索算法就是——穷举法,即枚举所有情况 ...

  9. 《MySQL必知必会》通配符 ( like , % , _ ,)

    <MySQL必知必会>通配符 ( like , % , _ ,) 关键字 LIke WHERE 搜索子句中使用通配符,必须使用 LIKE 操作符. % 百分号通配符 % 表示任意字符出现任 ...

  10. Airtest操作多台云手机,进行分发,多个脚本后端调度

    一.核心重点 使用python后端框架:tornado 调度:redis队列 存手机的class_name采取头取尾曾 多台手机连接方式 connect_device(f"Android:/ ...