最短路

很容易想到边和边之间连边,但是这样菊花图就完蛋了

我们想办法优化一下,能不能不要每条边都连。

考虑查分,把一个点的出边串起来,这样就行了,每条无向边拆成两条就能保证了

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + ;
namespace IO
{
const int Maxlen = N * ;
char buf[Maxlen], *C = buf;
int Len;
inline void read_in()
{
Len = fread(C, , Maxlen, stdin);
buf[Len] = '\0';
}
inline void fread(int &x)
{
x = ;
int f = ;
while (*C < '' || '' < *C) { if(*C == '-') f = -; ++C; }
while ('' <= *C && *C <= '') x = (x << ) + (x << ) + *C - '', ++C;
x *= f;
}
inline void fread(long long &x)
{
x = ;
long long f = ;
while (*C < '' || '' < *C) { if(*C == '-') f = -; ++C; }
while ('' <= *C && *C <= '') x = (x << ) + (x << ) + *C - '', ++C;
x *= f;
}
inline void read(int &x)
{
x = ;
int f = ; char c = getchar();
while(c < '' || c > '') { if(c == '-') f = -; c = getchar(); }
while(c >= '' && c <= '') { x = (x << ) + (x << ) + c - ''; c = getchar(); }
x *= f;
}
inline void read(long long &x)
{
x = ;
long long f = ; char c = getchar();
while(c < '' || c > '') { if(c == '-') f = -; c = getchar(); }
while(c >= '' && c <= '') { x = (x << 1ll) + (x << 3ll) + c - ''; c = getchar(); }
x *= f;
}
} using namespace IO;
int rd()
{
int x = , f = ;
char c = getchar();
while(c < '' || c > '') { if(c == '-') f = -; c = getchar(); }
while(c >= '' && c <= '') { x = x * + c - ''; c = getchar(); }
return x * f;
}
int n, cnt = , source, sink, m;
int head[N], hd[N * ], st[N], vis[N * ];
ll d[N * ];
struct edge {
int nxt, to, w;
} e[N * ];
bool cmp(int i, int j)
{
return e[i].w < e[j].w;
}
void link(int *head, int u, int v, int w)
{
e[++cnt].nxt = head[u];
head[u] = cnt;
e[cnt].to = v;
e[cnt].w = w;
}
int main()
{
read_in();
fread(n);
fread(m);
for(int i = ; i <= m; ++i)
{
int u, v, w;
fread(u);
fread(v);
fread(w);
link(head, u, v, w);
link(head, v, u, w);
}
source = ;
sink = * m + ;
for(int i = ; i <= n; ++i)
{
int top = ;
for(int j = head[i]; j; j = e[j].nxt) st[++top] = j;
sort(st + , st + top + , cmp);
for(int j = ; j <= top; ++j)
{
int now = st[j], nxt = st[j + ];
if(i == ) link(hd, source, now, e[now].w);
if(e[now].to == n) link(hd, now, sink, e[now].w);
link(hd, now, now ^ , e[now].w);
if(j < top)
{
link(hd, now, nxt, e[nxt].w - e[now].w);
link(hd, nxt, now, );
}
}
}
priority_queue<pair<ll, int>, vector<pair<ll, int> >, greater<pair<ll, int> > > q;
memset(d, 0x3f3f, sizeof(d));
d[source] = ;
q.push({, source});
while(!q.empty())
{
pair<ll, int> o = q.top();
q.pop();
int u = o.second;
if(vis[u]) continue;
vis[u] = ;
for(int i = hd[u]; i; i = e[i].nxt) if(d[e[i].to] > d[u] + e[i].w)
{
d[e[i].to] = d[u] + e[i].w;
q.push({d[e[i].to], e[i].to});
}
}
printf("%lld\n", d[sink]);
return ;
}

最新文章

  1. Pyunit测试框架
  2. 【eoe 6】ActionBar的使用
  3. zstu.4022.旋转数阵(模拟)
  4. HOWTO:制作 Windows 7 加速部署映像(作者:苏繁)
  5. 【原创】VNC-view配置
  6. javascript之css常用属性
  7. Ubuntu 16.04系统下出现E: 无法下载 http://ppa.launchpad.net/fcitx-team/nightly/ubuntu/dists/xenial/main/binary-amd64/Packages 404 Not Found
  8. ural 1297. Palindrome
  9. 打印杨辉三角--for循环
  10. zookeeper原理介绍
  11. jquery通过ajax查询数据动态添加到select
  12. linux 用户空间与内核空间——高端内存详解
  13. 查看文件状态与跟踪新文件(git status/add)
  14. [Java in NetBeans] Lesson 04. Class / Objects
  15. 【HAOI2011】problem b
  16. (转)Maven学习总结(五)——聚合与继承
  17. Western Subregional of NEERC, Minsk, Wednesday, November 4, 2015 Problem H. Parallel Worlds 计算几何
  18. div的最小宽高和最大宽高
  19. 教你在Android手机上使用全局代理
  20. Java基础-包(package)的声明与访问

热门文章

  1. odoo 的时差 坑
  2. 无线热点登陆认证原理探究---captive portal
  3. PE添加Style
  4. linux SPI驱动——简单的gpio模拟SPI驱动测试 (二)
  5. (Vue)vue模板语法
  6. AndroidPageObjectTest_Simple.java
  7. Cocos2d-x之CCMenu理解
  8. object-c中的assign,retain,copy,atomic,nonatomic,readonly,readwrite以及strong,weak
  9. Shell中括号的作用
  10. Tomcat Session Clustering