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 ...
随机推荐
- C++类、函数、指针
1.初始化所有指针. 2. (1)指向常量的指针: (2)常量指针:指针本身为常量: 3.若循环体内部包含有向vector对象添加元素的语句,则不能使用范围for循环. 4.字符数组要注意字符串字面值 ...
- 学学Viewbinding
Viewbinding 1.环境需求 环境上,需要Android Studio 3.6 Canary 11+ 同样的Gradle也需要升级(这年头都4.0了,应该没有还在用低版本的了吧...) 2.配 ...
- 自定制格式化方式format
自定制格式化方式format # x='{0}{0}{0}'.format('dog') # # print(x) # class Date: # def __init__(self,year,mon ...
- SSH全注解-annotation详细配置
web.xml的配置: <!--Spring的装载器 --> <listener> <listener-class> org.springframework.web ...
- VisualSVN Server修改默认端口号 443->8443
- Docker 启动 OpenResty
Docker 启动 OpenResty OpenResty 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库.第三方模块以及大多数的依赖项.用于方便地搭建 ...
- 《Spanner: Google’s Globally-Distributed Database》论文总结
Spanner 总结 说明:本文为论文 <Spanner: Google's Globally-Distributed Database> 的个人理解,难免有理解不到位之处,欢迎交流与指正 ...
- OpenCV之高斯平滑(Python实现)
假设一个列数为W,行数为H的高斯卷计算子gaussKernel,其中W,H均为奇数,描点位置在((H-1)/2 ,(W-1)/2),构建高斯卷积核的步骤如下 1.计算高斯矩阵 \[gaussMatri ...
- day27:异常&反射
目录 认识异常处理 1.程序错误的种类 2.异常的分类 3.AssertionError(断言assert语句失败) 异常处理的基本语法 1.异常处理的基本语法 2.带有分支的异常处理 3.处理 ...
- C#LeetCode刷题之#888-公平的糖果交换(Fair Candy Swap)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3758 访问. 爱丽丝和鲍勃有不同大小的糖果棒:A[i] 是爱丽丝 ...