当前位置:网站首页>洛谷P6822 [PA2012]Tax(最短路+边变点)
洛谷P6822 [PA2012]Tax(最短路+边变点)
2022-06-25 06:43:00 【mfy的1号小迷弟】
洛谷P6822 [PA2012]Tax(最短路+边变点)
题意:
给出一个 n 个点 m 条边的无向图,经过一个点的代价是进入和离开这个点的两条边的边权的较大值,求从起点 1到点 n 的最小代价。起点的代价是离开起点的边的边权,终点的代价是进入终点的边的边权。
思路:
暴力出奇迹,把所有的边看成点,若a->b,b>c,这将这两条边看成点连起来,最后建立超级源点S,于与1相连的点连起来,权值为0,建立超级汇点T,于与n相连的点连起来。共m^2条边。
考虑优化:
1.对于一个点,我们把它的出边从小到大排序
2.枚举每一条边,如果这条边连接着1或者N,那么我们从S连向这条边或者从这条边连向T,权值为该边的权值
3.从改边所对应的入边向该边连一条边,边权为它们的权值
4.枚举每一条出边,从权值较小的向权值较大的连权值为两边差值的边,从权值较大的向权值较小的连权值为0的边
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e6+5;
const int INF=0x3f3f3f;
typedef pair<ll,int>pii;
int n,m,k=1,k1=1,nex[maxn*2],head[maxn],to[maxn*2],ne[maxn*2];
int h[maxn],t[maxn*2],vis[maxn],p[maxn];
ll w[maxn*2],ww[maxn*2],dis[maxn];
void add(int x,int y,int z){
nex[++k]=head[x];
to[k]=y;
head[x]=k;
w[k]=z;
}
void pdd(int x,int y,int z){
ne[++k1]=h[x];
t[k1]=y;
h[x]=k1;
ww[k1]=z;
}
ll dij(){
priority_queue<pii,vector<pii>,greater<pii>>q;
for(int i=1;i<=maxn;i++)dis[i]=1e18;
dis[0]=0;
q.push({
0,0});
while(!q.empty()){
int x=q.top().second;
q.pop();
if(vis[x])continue;
vis[x]=1;
for(int i=h[x];i;i=ne[i]){
int y=t[i];
if(dis[y]>dis[x]+ww[i]){
dis[y]=dis[x]+ww[i];
q.push({
dis[y],y});
}
}
}
return dis[1];
}
bool cmp(int x,int y){
return w[x]<w[y];
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1,x,y,z;i<=m;i++){
scanf("%d%d%d",&x,&y,&z);
add(x,y,z),add(y,x,z);
pdd(k,k^1,z),pdd(k^1,k,z);
}
for(int i=1;i<=n;i++){
int d=0;
for(int j=head[i];j;j=nex[j])p[++d]=j;
sort(p+1,p+d+1,cmp);
for(int j=1;j<=d;j++){
if(j!=1){
pdd(p[j-1],p[j],w[p[j]]-w[p[j-1]]);
pdd(p[j],p[j-1],0);
}
if(i==1)pdd(0,p[j],w[p[j]]);
else if(i==n)pdd(p[j]^1,1,w[p[j]]);
}
}
printf("%lld\n",dij());
}
边栏推荐
- 將數據導入到MATLAB
- Startup, shutdown and restart of Oracle and MySQL on Linux
- 如何用svn新建属于自己的分支
- Application of can optical transceiver of ring network redundant can/ optical fiber converter in fire alarm system
- Five causes of PCB board deformation and six solutions 2021-10-08
- FM信号、调制信号和载波
- c# winform panel自定义图片和文字
- Analysis of kinsing dual platform mining family virus
- What are the problems with traditional IO? Why is zero copy introduced?
- 力扣76题,最小覆盖字串
猜你喜欢

消息中间件之ActiveMQ的基本使用

Fairmot yolov5s to onnx

"Spatial transformation" significantly improves the quality of ground point extraction of cliff point cloud

【深度学习 轻量型backbone】2022 EdgeViTs CVPR
![[deep learning lightweight backbone] 2022 edgevits CVPR](/img/13/139d28621403020e3475a30f6148f8.png)
[deep learning lightweight backbone] 2022 edgevits CVPR

Pcb|about FPC reinforcement type

Import data into Matlab

新版USBCAN卡CAN分析仪的CAN&CANFD综合测试分析软件LKMaster主要功能介绍

Hisilicon 3559 sample parsing: Vio

Four software 2021-10-14 suitable for beginners to draw PCB
随机推荐
单位转换-毫米转像素-像素转毫米
Modular programming of LCD1602 LCD controlled by single chip microcomputer
Take you through the normalization flow of GaN
NPM install reports an error: gyp err! configure error
【补题】2021牛客暑期多校训练营1-3
微信小程序入门记录
Usememo simulation usecallback
One "stone" and two "birds", PCA can effectively improve the dilemma of missing some ground points under the airborne lidar forest
2265. number of nodes with statistical value equal to the average value of subtree
Atlassian confluence漏洞分析合集
50. Pow(x, n)-快速幂
Misunderstanding of switching triode
网络模型——OSI模型与TCP/IP模型
判断用户是否是第一次进入某个页面
Import data into Matlab
【QT】Qt 5 的程序:打印文档
Can transparent cloud gateway caniot and candtu record can messages and send and receive can data remotely
1464. maximum product of two elements in an array
微信小程序开通客服消息功能开发
CAN透传云网关CANIOT,CANDTU记录CAN报文远程收发CAN数据