Codeforces 938D Buy a Ticket

题目链接http://codeforces.com/problemset/problem/938/D

题意:给定一张无向图,每个节点有自己的点权a_i,每条边有一个边权w,对于每个点i求出最小的2\* dis(i,j)+a[j],其中dis(i,j)ij的最短路。

思路:设立超级源点,从源点向每个点连一条边权为对应点权即a_i的边,然后每条边的边权乘2,跑一个单源最短路,则每个点到源点的最短路就是我们要求的答案。

#pragma comment(linker, "/STACK:102400000,102400000")
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
#define Key_Value ch[ch[root][1]][0]
#define DBN1(a)           cerr<<#a<<"="<<(a)<<"\n"
#define DBN2(a,b)         cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<"\n"
#define DBN3(a,b,c)       cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<"\n"
#define DBN4(a,b,c,d)     cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<"\n"
#define DBN5(a,b,c,d,e)   cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<"\n"
#define DBN6(a,b,c,d,e,f) cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<", "<<#f<<"="<<(f)<<"\n"
#define clr(a,x) memset(a,x,sizeof(a))
#define pb push_back
#define mp make_pair
#define ALL(x) x.begin(),x.end()
#define F first
#define S second
using namespace std;
typedef long long ll;
const int maxn=500000+5;
const int INF=0x3f3f3f3f;
const int P=1000000007;
const double PI=acos(-1.0);
template<typename T>
inline T read(T&x){
    x=0;int _f=0;char ch=getchar();
    while(ch<'0'||ch>'9')_f|=(ch=='-'),ch=getchar();
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    return x=_f?-x:x;
}
template <class T1, class T2>inline void gmax(T1 &a,T2 b){if (b>a) a=b;}
template <class T1, class T2>inline void gmin(T1 &a,T2 b){if (b<a) a=b;}
void up(int&x,int y){x+=y;if(x>=P)x-=P;}
const int N=2e5+10;
struct Edge{
    int v;ll w;
};
struct HeapNode{
    int u;ll d;
    bool operator<(const HeapNode&rhs)const{
        return d>rhs.d;
    }
};
struct Dijkstra{
    int n;
    bool vis[N];
    ll dis[N];
    vector<Edge>G[N];

    void init(int n){
        this->n=n;
        for (int i=1;i<=n;i++) G[i].clear();
    }
    void AddEgde(int u,int v,ll w){
        G[u].pb((Edge){v,w});
        G[v].pb((Edge){u,w});
    }
    void dijkstra(){
        priority_queue<HeapNode>Q;
        for (int i=1;i<=n;i++) Q.push((HeapNode){i,dis[i]});
        memset(vis,false,sizeof(vis));
        while (!Q.empty()){
            HeapNode cur=Q.top();Q.pop();
            int u=cur.u;
            if (vis[u]) continue;
            vis[u]=true;
            for (int i=0;i<(int)G[u].size();i++){
                int v=G[u][i].v;ll w=G[u][i].w;
                if (dis[v]>dis[u]+w){
                    dis[v]=dis[u]+w;
                    Q.push((HeapNode){v,dis[v]});
                }
            }
        }
    }
}A;
int n,m,u,v;ll w;
int main(){
    read(n),read(m);
    A.init(n);
    for (int i=1;i<=m;i++){
        read(u),read(v),read(w);
        A.AddEgde(u,v,2LL*w);
    }
    for (int i=1;i<=n;i++){
        read(A.dis[i]);
    }
    A.dijkstra();
    for (int i=1;i<=n;i++) printf("%lld%c",A.dis[i],i==n?'\n':' ');
    return 0;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注