当前位置:网站首页>洛谷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());
}
边栏推荐
- 【视频】ffplay 使用mjpeg格式播放usb摄像头
- Understand the reasons for impedance matching of PCB circuit board 2021-10-07
- 【论文学习】《VQMIVC》
- SSL证书免费获取教程
- 用函数的递归来解决几道有趣的题
- Six causes of PCB disconnection 2021-10-20
- MySQL interview - the response of executing SQL is relatively slow, and the troubleshooting ideas.
- C control refresh
- 微信小程序开通客服消息功能开发
- 【红旗杯?】补题
猜你喜欢

【论文学习】《VQMIVC》

How much do you know about electronic components on PCB?

Tips on how to design soft and hard composite boards ~ 22021/11/22

Can bus working condition and signal quality "physical examination"

Requirements for Power PCB circuit board design 2021-11-09

SCM Project Training

OAuth 2.0 one click login

This article uses pytorch to build Gan model!

挖掘微生物暗物质——新思路

Terms and concepts related to authority and authentication system
随机推荐
Introduction to the main functions of the can & canfd comprehensive test and analysis software lkmaster of the new usbcan card can analyzer
差点被这波Handler 面试连环炮带走~
c# winform panel自定义图片和文字
Understand the reasons for impedance matching of PCB circuit board 2021-10-07
Modular programming of oled12864 display controlled by single chip microcomputer
Apache CouchDB 代码执行漏洞(CVE-2022-24706 )批量POC
The fourth floor is originally the fourth floor. Let's have a look
Share the process requirements for single-layer flexible circuit board
Find out what informatization is, and let enterprises embark on the right path of transformation and upgrading
[Video] ffplay uses MJPEG format to play USB camera
【深度学习 轻量型backbone】2022 EdgeViTs CVPR
OpenCV每日函数 结构分析和形状描述符(8) fitLine函数 拟合直线
C#控件刷新
Modular programming of wireless transmission module nRF905 controlled by single chip microcomputer
Six causes of PCB disconnection 2021-10-20
1464. 数组中两元素的最大乘积
LeetCode_哈希表_中等_454.四数相加 II
Importer des données dans MATLAB
417-二叉树的层序遍历1(102. 二叉树的层序遍历、107.二叉树的层次遍历 II、199.二叉树的右视图、637.二叉树的层平均值)
判断用户是否是第一次进入某个页面