BZOJ4289 PA2012 Tax
作者:互联网
Problem
Description
给出一个N个点M条边的无向图,经过一个点的代价是进入和离开这个点的两条边的边权的较大值,求从起点1到点N的最小代价。起点的代价是离开起点的边的边权,终点的代价是进入终点的边的边权
N<=100000
M<=200000
Sample Input
4 5
1 2 5
1 3 2
2 3 1
2 4 4
3 4 8
Sample Output
12
Solution
//lzjtxdy
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int N=2000000,M=2000000;
int head[N],ver[M],nxt[M],edge[M],tot=0;
int Head[N],Ver[M],Nxt[M],Edge[M],Tot=0;
bool book[N];
long long dis[N];
#define s(x) ((x)%2==1?((x)+1):((x)-1))
struct node
{
int z;
int pos;
}tmp[N];
void add(int x,int y,int z)
{
ver[++tot]=y;
edge[tot]=z;
nxt[tot]=head[x];
head[x]=tot;
}
void addedge(int x,int y,int z)
{
Ver[++Tot]=y;
Edge[Tot]=z;
Nxt[Tot]=Head[x];
Head[x]=Tot;
}
void dijkstra(int st)
{
priority_queue<pair<long long,int> > que;
que.push(make_pair(0,st));
memset(dis,0x3f,sizeof(dis));
dis[st]=0;
memset(book,false,sizeof(book));
while(!que.empty())
{
int x=que.top().second; que.pop();
if(book[x]) continue;
book[x]=true;
for(int i=head[x];i;i=nxt[i])
{
int y=ver[i],z=edge[i];
if(dis[x]+z<dis[y])
{
dis[y]=dis[x]+z;
que.push(make_pair(-dis[y],y));
}
}
}
}
bool cmp(node a,node b) {return a.z<b.z;}
void Add(int x)
{
int cnt=0;
for(int i=Head[x];i;i=Nxt[i])
{
tmp[++cnt].pos=i;
tmp[cnt].z=Edge[i];
}
sort(tmp+1,tmp+cnt+1,cmp);
for(int i=1;i<cnt;i++)
{
add(tmp[i].pos,tmp[i+1].pos,tmp[i+1].z-tmp[i].z);
add(tmp[i+1].pos,tmp[i].pos,0);
}
for(int i=Head[x];i;i=Nxt[i]) add(s(i),i,Edge[i]);
}
int main()
{
int n,m;
scanf("%d %d",&n,&m);
for(int i=1;i<=m;i++)
{
int xx,yy,zz;
scanf("%d %d %d",&xx,&yy,&zz);
addedge(xx,yy,zz);
addedge(yy,xx,zz);
}
int S=0,T=Tot+1;
for(int i=Head[1];i;i=Nxt[i]) add(S,i,Edge[i]);
for(int i=Head[n];i;i=Nxt[i]) add(s(i),T,Edge[i]);
for(int i=1;i<=n;i++) Add(i);
dijkstra(S);
printf("%lld",dis[T]);
return 0;
}
标签:int,Tax,PA2012,tot,que,Tot,include,BZOJ4289,dis 来源: https://www.cnblogs.com/juruo-zzt/p/12810924.html