HDU - 3499 -(Dijkstra变形+枚举边)
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变形+枚举边)的更多相关文章
- NYOJ 1248 海岛争霸(Dijkstra变形——最短路径最大权值)
题目链接: http://acm.nyist.net/JudgeOnline/problem.php?pid=1248 描述 神秘的海洋,惊险的探险之路,打捞海底宝藏,激烈的海战,海盗劫富等等.加勒比 ...
- POJ.1797 Heavy Transportation (Dijkstra变形)
POJ.1797 Heavy Transportation (Dijkstra变形) 题意分析 给出n个点,m条边的城市网络,其中 x y d 代表由x到y(或由y到x)的公路所能承受的最大重量为d, ...
- 【lightoj-1002】Country Roads(dijkstra变形)
light1002:传送门 [题目大意] n个点m条边,给一个源点,找出源点到其他点的‘最短路’ 定义:找出每条通路中最大的cost,这些最大的cost中找出一个最小的即为‘最短路’,dijkstra ...
- hdu 3499 Flight (最短路径)
Flight Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Su ...
- 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 ...
- hdu 3499 flight 【分层图】+【Dijkstra】
<题目链接> 题目大意: 现在给你一些点,这些点之间存在一些有向边,每条边都有对应的边权,有一次机会能够使某条边的边权变为原来的1/2,求从起点到终点的最短距离. 解题分析: 分层图最短路 ...
- 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 ...
- HDU 5778 abs (枚举)
abs 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5778 Description Given a number x, ask positive ...
- HDU 2112 HDU Today (Dijkstra算法)
HDU Today Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
随机推荐
- iOS alertController自带的输入框
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@" ...
- markdown公式指导手册
#Cmd Markdown 公式指导手册 标签: Tutorial 转载于https://www.zybuluo.com/codeep/note/163962#1%E5%A6%82%E4%BD%95% ...
- 标星7000+,这个 Python 艺术二维码生成器厉害了!
微信二维码,相信大家也并不陌生,为了生成美观的二维码,许多用户都会利用一些二维码生成工具. 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手 ...
- C#LeetCode刷题之#66-加一(Plus One)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3684 访问. 给定一个由整数组成的非空数组所表示的非负整数,在该 ...
- 怎么写简历,简历才不会被丢到非洲🌍
前言 只有光头才能变强. 文本已收录至我的GitHub精选文章,欢迎Star:https://github.com/ZhongFuCheng3y/3y 最近的三歪朋友圈可以看到很多的字节.腾讯的同学都 ...
- 攻防世界-web(进阶)-Training-WWW-Robots
进行后台扫描,发现一个robots.txt,进入之后说存在fl0g.php,进入即可得flag. cyberpeace{73279bc0d3c28ba6da4d1d3d530e7c16}
- MyKTV系统项目的感想
不粉身碎骨,何以脱胎换骨! 3月11号,我们迎来S1的尾巴.这期间有温暖,默契,有项目.一切刚刚好.刚刚正式接到KTV这个微微型的项目的时候,还是很害怕的,虽然老师在前两天就已经提到也讲到,KTV系统 ...
- Elasticsearch第五篇:PlainElastic.Net 操作 Elasticsearch
再次强调,我安装的Elasticsearch 版本是 7.8.0 ,C# 操作 Elasticsearch 的驱动有 NEST.Elasticsearch.net .PlainElastic.Net ...
- python库安装失败的解决方法
安装python库 在https://www.lfd.uci.edu/~gohlke/pythonlibs 中,搜索对应库名称 选取对应版本下载 在cmd窗口中,用命令 pip install+文件路 ...
- @SuppressWarnings注解用法详解(转)
原文连接https://blog.csdn.net/sysware_carol/article/details/52100580 今天来谈谈@SuppressWarnings注解的作用. J2SE 提 ...