[题目链接](http://acm.hdu.edu.cn/showproblem.php?pid=3499)

刚看见题目,哦?不就是个最短路么,来,跑一下dijkstra记录最长路除个二就完事了 ,但是。。。。。。

走红色的路是最佳方案,但是蓝色路的最短路跟短,我想错了;

不久我又想,把每个边枚举一下,减半一个边,就跑一次dijstra,但是这太慢了,O(m*m*logm)

拜拜了您內

后来看了看网上的大佬,其实也可以枚举嘛,只要每一次枚举都是O(1)。显然需要预处理一下,正线边跑一次,反向边跑一次

这样就可以了

听说还可以分层图,没看懂,猜测了一下原理

就这样了

#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
#include<queue>
#include<map>
#include<string>
using namespace std;
typedef long long ll;
const int maxn = 5e5 + 100;
const long long INF = 1e12;
struct Node {
ll p, val;
Node(ll a, ll b) :p(a), val(b) {}
};
bool operator <(const Node a, const Node b) {
if (a.val > b.val) return true;
else return false;
}
int n, m;
ll d1[maxn];
ll d2[maxn];
int vis[maxn];
vector<Node>G[maxn];
void insert(int be, int en, int len) {
G[be].push_back(Node(en, len));
return;
}
int dijstra(ll *dis, int be) {
for (int i = 0; i <= n; i++) {
vis[i] = 0;
dis[i] = INF;
}
dis[be] = 0;
priority_queue<Node>que;
que.push(Node(be, 0));
while (!que.empty()) {
Node ans = que.top();
que.pop();
if (vis[ans.p] == 0) {
vis[ans.p] = 1;
for (int i = 0; i < G[ans.p].size(); i++) {
int p = G[ans.p][i].p;
if (!vis[p] && dis[p] > dis[ans.p] + G[ans.p][i].val) {
dis[p] = dis[ans.p] + G[ans.p][i].val;
que.push(Node(p, dis[p]));
}
}
}
}
return 0;
}
map<string, int>ins;
ll be_[maxn];
ll en_[maxn];
ll len_[maxn];
int main() {
while (~scanf("%d %d", &n, &m)) {
ins.clear();//可得记得清空啊
string be, en;
ll len = 0;
int c = 0;
int cnt = 1;
while (m--) {
cin >> be >> en;
scanf("%lld", &len);
if (ins[be] == 0) ins[be] = cnt++;
if (ins[en] == 0) ins[en] = cnt++;
be_[c] = ins[be];
en_[c] = ins[en];
len_[c] = len;
c++;
insert(ins[en], ins[be], len);
}
cin >> be >> en;
if (ins[be] == 0) ins[be] = cnt++;
if (ins[en] == 0) ins[en] = cnt++;
dijstra(d2, ins[en]);
for (int i = 0; i <= n; i++) G[i].clear();
for (int i = 0; i < c; i++) {
insert(be_[i], en_[i], len_[i]);
}
dijstra(d1, ins[be]);
ll ans = d1[ins[en]];
if (ans == INF) {
printf("-1\n");
continue;
}
for (int i = 1; i <= n; i++) {
for (int j = 0; j < G[i].size(); j++) {
ans = min(ans, d1[i] + d2[G[i][j].p] + G[i][j].val / 2);//每个顶点刷出来边试探
}
} printf("%lld\n", ans);
for (int i = 0; i <= n; i++) G[i].clear();
}
return 0;
}

  

选择合适的最短路--hdu3499的更多相关文章

  1. NoSQL系列:选择合适的数据库

    NoSQL系列:选择合适的数据库 为什么使用NoSQL数据库? 阻抗失衡 关系模型和内存中的数据结构不匹配 采用更为方便的数据交互方式提升开发效率 待处理的数据量很大 数据量超过关系型数据库的承载能力 ...

  2. 为MySQL选择合适的备份方式

    数据库的备份是极其重要的事情.如果没有备份,遇到下列情况就会抓狂: UPDATE or DELETE whitout where… table was DROPPed accidentally… IN ...

  3. (转)NoSQL系列:选择合适的数据库

    内容目录: 为什么使用NoSQL数据库? 键值数据库 文档数据库 列族数据库 图数据库 附思维导图 参考 NoSQL系列:选择合适的数据库 为什么使用NoSQL数据库? 阻抗失衡 关系模型和内存中的数 ...

  4. (转载)通过dbgrideh 从数据集中选择合适的记录

    通过dbgrideh 从数据集中选择合适的记录 //---------------------------------------------------------// 通过dbgrideh 从数据 ...

  5. 【转】app后端如何选择合适的数据库产品

    转自:http://blog.csdn.net/newjueqi/article/details/44003503 app后端的开发中,经常要面临的一个问题是:数据放在哪里? mysql ?redis ...

  6. 谈谈数据库中MyISAM与InnoDB区别 针对业务类型选择合适的表

    MyISAM:这个是默认类型,它是基于传统的ISAM类型, ISAM是Indexed Sequential Access Method (有索引的顺序访问方法) 的缩写,它是存储记录和文件的标准方法. ...

  7. 如何选择合适的PHP开发框架

    PHP作为一门成熟的WEB应用开发语言,已经深受广大开发者的青睐.与此同时,各式各样的PHP开发框架也从出不穷,面对如此多而且良莠不齐的开发框架,开发者们想必都会眼花缭乱,不知道该选择用哪个.其实并没 ...

  8. MySQL如何选择合适的引擎以及引擎的转换。

    我们怎么选择合适的引擎?这里简单归纳一句话:"除非需要用到某些InnoDB不具备的特性,并且没有其他办法可以替代,否则都应该优先选择InnoDB引擎." 除非万不得已,否则不建议混 ...

  9. 为MySQL选择合适的备份方式[转]

    原文链接:http://nettedfish.sinaapp.com/blog/2013/05/31/choose-suitable-backup-strategy-for-mysql/ 数据库的备份 ...

随机推荐

  1. 一 linux安装python3

    参考 https://www.cnblogs.com/pyyu/p/7402145.html?tdsourcetag=s_pcqq_aiomsg 1 下载 网址:https://www.python. ...

  2. macbook Air安装OS系统,提示“请插入电源适配器”,实际已插电源却检测不到

    在重做Mac系统时需要插电源是众所周知的,但在同意协议之后,选择安装盘下一步时提示“请插入电源适配器”??WTF! 明明电源已经插上了却检测不到......气绝 解决方案:按住组合件"shi ...

  3. zip解决杨辉三角问题

    杨辉三角原型: / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ 实现: a = [1] while True: print(a) ...

  4. struts.xml中的结果类型与视图

    实际上在Struts2框架中,一个完整的结果视图配置文件应该是: ? 1 2 3 4 5 <action name="Action名称" class="Action ...

  5. PHP 手机短信验证码 laravel 实现流程

    https://blog.csdn.net/uknow0904/article/details/80336941 本人在自己博客(Laravel)的注册部分 使用手机号注册,需要发送短信验证码. 使用 ...

  6. ODT 珂朵莉树 入门

    #include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> ...

  7. hdu 1286 找新朋友 (容斥原理 || 欧拉函数)

    Problem - 1286 用容斥原理做的代码: #include <cstdio> #include <iostream> #include <algorithm&g ...

  8. C# Brush Color String 互相转换

    using System.Windows.Media; //String转换成Color Color color = (Color)ColorConverter.ConvertFromString(s ...

  9. 洛谷P1488 肥猫的游戏 题解 博弈论入门

    题目链接:https://www.luogu.org/problem/P1488 其实这道题目我只需要 \(n\) 以及黑色三角形的三个端点编号就可以了. 我们假设在一个 \(n\) 边形中,黑色三角 ...

  10. hdu 2986 Ballot evaluation (Simulation)

    Problem - 2986 之前在华工赛见过的一道简单的模拟,用map轻松干掉.为了精确,要全程用整型比较.轻松1y~ 代码如下: #include <cstdio> #include ...