首页 > 其他 > 详细

【刷题】BZOJ 4289 PA2012 Tax

时间:2018-09-21 22:50:18      阅读:169      评论:0      收藏:0      [点我收藏+]

Description

给出一个N个点M条边的无向图,经过一个点的代价是进入和离开这个点的两条边的边权的较大值,求从起点1到点N的最小代价。起点的代价是离开起点的边的边权,终点的代价是进入终点的边的边权

N<=100000

M<=200000

Input

Output

Sample Input

4 5
1 2 5
1 3 2
2 3 1
2 4 4
3 4 8

Sample Output

12

Solution

这种边与边之间有特殊贡献的题目一般都是拆边为点
这题把每条边拆成两个点,把原来的点周围的边拆出的点排序后从权值低的向高的连权值为权值差的边,反向则连权值为 \(0\) 的边
看一看程序画一画就好了

#include<bits/stdc++.h>
#define ui unsigned int
#define ll long long
#define db double
#define ld long double
#define ull unsigned long long
#define REP(a,b,c) for(register int a=(b),a##end=(c);a<=a##end;++a)
#define DEP(a,b,c) for(register int a=(b),a##end=(c);a>=a##end;--a)
const int MAXN=400000+10,MAXM=1000000+10;
const ll inf=1e18;
int n,m,beg[MAXN],nex[MAXM<<1],to[MAXM<<1],was[MAXM<<1],e;
ll d[MAXN];
std::priority_queue< std::pair<ll,int>,std::vector< std::pair<ll,int> >,std::greater< std::pair<ll,int> > > q;
std::vector<int> t;
std::vector< std::pair<int,int> > G[MAXN];
template<typename T> inline void read(T &x)
{
    T data=0,w=1;
    char ch=0;
    while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
    if(ch=='-')w=-1,ch=getchar();
    while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();
    x=data*w;
}
template<typename T> inline void write(T x,char ch='\0')
{
    if(x<0)putchar('-'),x=-x;
    if(x>9)write(x/10);
    putchar(x%10+'0');
    if(ch!='\0')putchar(ch);
}
template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);}
template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);}
template<typename T> inline T min(T x,T y){return x<y?x:y;}
template<typename T> inline T max(T x,T y){return x>y?x:y;}
inline void insert(int x,int y,int z)
{
    to[++e]=y;
    nex[e]=beg[x];
    beg[x]=e;
    was[e]=z;
}
#define ft first
#define sd second
inline bool cmp(std::pair<int,int> A,std::pair<int,int> B)
{
    return A.sd<B.sd;
}
inline void build()
{
    REP(i,1,n)
    {
        std::sort(G[i].begin(),G[i].end(),cmp);
        REP(j,1,G[i].size()-1)
        {
            int u=G[i][j].ft,v=G[i][j-1].ft,w=G[i][j].sd-G[i][j-1].sd;
            insert(u,v,w);insert(v,u,0);
        }
    }
}
inline ll Dijkstra()
{
    REP(i,1,m<<1|1)d[i]=inf;
    REP(i,0,G[1].size()-1)
    {
        d[G[1][i].ft]=0;
        q.push(std::make_pair(0,G[1][i].ft));
    }
    while(!q.empty())
    {
        int x=q.top().sd;
        if(q.top().ft!=d[x])
        {
            q.pop();
            continue;
        }
        q.pop();
        for(register int i=beg[x];i;i=nex[i])
            if(d[to[i]]>d[x]+was[i])
            {
                d[to[i]]=d[x]+was[i];
                q.push(std::make_pair(d[to[i]],to[i]));
            }
    }
    ll ans=inf;
    REP(i,0,G[n].size()-1)chkmin(ans,d[G[n][i].ft]+G[n][i].sd);
    return ans;
}
#undef ft
#undef sd
int main()
{
    read(n);read(m);
    REP(i,1,m)
    {
        int u,v,w;read(u);read(v);read(w);
        if(u>v)std::swap(u,v);
        insert(i<<1,i<<1^1,w);insert(i<<1^1,i<<1,w);
        G[v].push_back(std::make_pair(i<<1^1,w));
        G[u].push_back(std::make_pair(i<<1,w));
    }
    build();
    write(Dijkstra(),'\n');
    return 0;
}

【刷题】BZOJ 4289 PA2012 Tax

原文:https://www.cnblogs.com/hongyj/p/9688400.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!