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 ...
随机推荐
- 安卓APP开发的初步了解
今天成功安装了Android Studio 并且对APP的开发框架结构进行了初步了解 如上图:app基本结构情况 下面来仔细解释一下各个方面目录的作用 首先 manifests目录:包含Android ...
- 朴素贝叶斯分类器基本代码 && n折交叉优化 2
这个代码基于上一个代码 不同的是:读取了txt文件,改变了min_ft与max_ft的参数 import re import pandas as pd import warnings import n ...
- 16、Java中级进阶 面向对象 封装
1.封装概述 封装可以被认为是一个保护屏障,将类的某些信息隐藏在类内部,不允许外部程序直接访问,而是通过类提供的方法来实现对隐藏信息的操作访问,可以有效的防止该类的代码和数据被其他类随意访问. 要访问 ...
- JDK 1.8 中文 API CHM
链接: https://pan.baidu.com/s/1AiJn6RM1KoEL1n_96qoQhQ 提取码: n2ya
- Windows10上安装Linux子系统(WSL2,Ubuntu),配合Windows Terminal使用
Linux 的 Windows 子系统可让开发人员按原样运行 GNU/Linux 环境 - 包括大多数命令行工具.实用工具和应用程序 - 且不会产生传统虚拟机或双启动设置开销. WSL 说白了安装Li ...
- 对象原型之__proto__
对象都会有一个__proto__指向构造函数的prototype原型对象,对象之所以能够使用构造函数的prototype原型对象的方法,就是因为有__proto__原型的存在. funct ...
- LeetCode.516 最长回文子序列 详解
题目详情 给定一个字符串s,找到其中最长的回文子序列.可以假设s的最大长度为1000. 示例 1: 输入: "bbbab" 输出: 4 一个可能的最长回文子序列为 "bb ...
- MongoDB学习2:MongoDB的基本操作
以下都是基于MongoShell进行操作 1.使用insert进行插入操作 示例: db.<集合>.insertOne(<JSON对象>) db.<集合>.ins ...
- 树堆(Treap)学习笔记 2020.8.12
如果一棵二叉排序树的节点插入的顺序是随机的,那么这样建立的二叉排序树在大多数情况下是平衡的,可以证明,其高度期望值为 \(O( \log_2 n )\).即使存在一些极端情况,但是这种情况发生的概率很 ...
- Flask实现RESTful API(注意参数位置解析)
准备工作 首先安装flask_restful三方组件 pip install flask_restful 在models.py中新建一个类,生成表,往里面插入一些数据.(flask要想使用ORM的话需 ...