bzoj4289
最短路
很容易想到边和边之间连边,但是这样菊花图就完蛋了
我们想办法优化一下,能不能不要每条边都连。
考虑查分,把一个点的出边串起来,这样就行了,每条无向边拆成两条就能保证了
#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 ;
}
bzoj4289的更多相关文章
- BZOJ4289 PA2012Tax(最短路)
一个暴力的做法是把边看成点,之间的边权为两边的较大权值,最短路即可.但这样显然会被菊花图之类的卡掉. 考虑优化建图.将边拆成两个有向边,同样化边为点.原图中同一条边在新图中的两个点之间连边权为原边权的 ...
- 【BZOJ-4289】Tax 最短路 + 技巧建图
4289: PA2012 Tax Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 168 Solved: 69[Submit][Status][Dis ...
- BZOJ4289 : PA2012 Tax
一个直观的想法是把每条边拆成两条有向边,同时每条有向边是新图中的一个点.对于两条边a->b与b->c,两点之间连有向边,费用为两条边费用的最大值.然后新建源点S与汇点T,由S向所有起点为1 ...
- BZOJ4289 Tax 最短路建模
给定一个带边权的无向图,求1到n的最小代价路径.经过一个点的代价是路径上这个点的入边和出边的较大权值. \(n \le 100000, m \le 200000\). 一般的建图是考虑每个点,其入边和 ...
- bzoj4289 Tax
Description 给出一个N个点M条边的无向图,经过一个点的代价是进入和离开这个点的两条边的边权的较大值,求从起点1到点N的最小代价.起点的代价是离开起点的边的边权,终点的代价是进入终点的边的边 ...
- [BZOJ4289][PA2012]TAX(最短路)
首先考虑一种暴力做法,为每条边拆成两条有向边,各建一个点.若某两条边有公共点,则在边所对应的点之间连一条边,权值为两条边中的较大值.这样跑最短路是$O(m^2\log m)$的. 用类似网络流中补流的 ...
- [Bzoj4289]PA2012 Tax(Dijkstra+技巧建图)
Description 给出一个N个点M条边的无向图,经过一个点的代价是进入和离开这个点的两条边的边权的较大值,求从起点1到点N的最小代价.起点的代价是离开起点的边的边权,终点的代价是进入终点的边的边 ...
- 【PA2012】【BZOJ4289】Tax
Description 给出一个N个点M条边的无向图,经过一个点的代价是进入和离开这个点的两条边的边权的较大值.求从起点1到点N的最小代价. 起点的代价是离开起点的边的边权.终点的代价是进入终点的边的 ...
- bzoj4289 PA2012 Tax——点边转化
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4289 好巧妙的转化!感觉自己难以想出来... 参考了博客:https://blog.csdn ...
随机推荐
- react webapp 开发小结
1.监听props的方法 componentWillReceiveProps(nextProps) { // } 2.监听state的方法 3.props 传递的方法 <AlarmList {. ...
- Ubuntu下编译Poco库
本文主要记录下Ubuntu下编译Poco C++库的配置项以备后用.系统版本:Ubuntu 16.04,1 Poco 版本:1.9.0基本的步骤如下: 1.从官网下载最新的Poco源码,地址是:htt ...
- Fighting regressions with git bisect---within git bisect algorithm
https://www.kernel.org/pub/software/scm/git/docs/git-bisect-lk2009.html Fighting regressions with gi ...
- Cocos2d-x 3.0的启动流程
Cocos2d-x 3.0变动非常大,包含启动的方式,我看了下对android的启动总结例如以下: Java方面: AppActivity继承Cocos2dxActivity Coco ...
- VCC/AVCC/VDD/AVDD区别
V*与AV*的区别是:数字与模拟的区别CC与DD的区别是:供电电压与工作电压的区别(通常VCC>VDD): 数字电路供电VCC 模拟电路供电AVCCVDD是指工作电压,就是供电进芯片的 AVDD ...
- PE添加Style
1. <style id="NumberStyle"> <setting> <param name="option"> ...
- Android自动折行TextView Group
package com.test.testview; import java.util.ArrayList; import android.content.Context; import androi ...
- EasyDarwin开源流媒体云平台之语音对讲功能设计与实现
本文由EasyDarwin开源团队成员Alex贡献:http://blog.csdn.net/cai6811376/article/details/52006958 EasyDarwin云平台一直在稳 ...
- cpio
1 压缩 -o,生成cpio格式的归档文件.从标准输入获取文件名列表. 2 解压 -i,对cpio格式的归档文件进行解压,生成单个的文件. 3 --null 从标准输入获取的文件名列表为"\ ...
- HDFS被设计成能够在一个大集群中跨机器可靠地存储超大文件
HDFS被设计成能够在一个大集群中跨机器可靠地存储超大文件.它将每个文件存储成一系列的数据块,除了最后一个,所有的数据块都是同样大小的.为了容错,文件的所有数据块都会有副本.每个文件的数据块大小和副本 ...