「POI 2003」 Smugglers

题意

给定 n n n 种金属的价格,带任意一种金属过境都要交其价格 50 % 50\% 50% 的税。

m m m 种关系,每种可以把第 a i a_i ai 种金属单向转换成第 b i b_i bi 种金属,花费 c i c_i ci 的代价。你可以在过境前后进行任意次转换。

最开始你有第 1 1 1 种金属,并需要带第 1 1 1 种金属过境,求最小代价。

分析

比较明显的最短路。

i i i 为第 i i i 种金属过境前的编号; i + n i+n i+n 为第 i i i 种金属过境后的编号。

i i i i + n i+n i+n 连边,边权为金属价格的一半。

a a a b b b a + n a+n a+n b + n b+n b+n 连边,边权为 c c c

从 1 跑最短路,答案为 d i s n + 1 dis_{n+1} disn+1

用 Dijkstra,时间复杂度为 O ( ( n + m ) log ⁡ ( n + m ) ) O((n+m)\log (n+m)) O((n+m)log(n+m))

Code

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
inline ll read(){ll x=0,f=1;char c=getchar();while(c<48||c>57){if(c==45)f=0;c=getchar();}while(c>47&&c<58)x=(x<<3)+(x<<1)+(c^48),c=getchar();return f?x:-x;}
const ll maxn=5005;
ll n,m,head[maxn<<1],tot,dis[maxn<<1];
bool vis[maxn<<1];
struct edge{ll to,nxt,val;}e[300005];
struct node{
    ll dis,pos;
    inline bool operator<(const node& b)const{
        return b.dis<dis;
    }
};
inline void add(ll u,ll v,ll w){
    e[++tot]=(edge){v,head[u],w};
    head[u]=tot;
}
inline void dij(ll s){
    memset(dis,0x3f,sizeof(dis));
    dis[s]=0;
    priority_queue<node>q;
    q.push((node){0,s});
    while(!q.empty()){
        node u=q.top();q.pop();
        if(vis[u.pos])continue;
        vis[u.pos]=1;
        for(ll i=head[u.pos];i;i=e[i].nxt){
            ll v=e[i].to;
            if(dis[u.pos]+e[i].val<dis[v]){
                dis[v]=dis[u.pos]+e[i].val;
                if(!vis[v])q.push((node){dis[v],v});
            }
        }
    }
}
signed main(){
    n=read();
    for(ll i=1;i<=n;++i)add(i,i+n,read()/2);
    m=read();
    while(m--){
        ll u=read(),v=read(),w=read();
        add(u,v,w),add(u+n,v+n,w);
    }
    dij(1);
    printf("%lld",dis[n+1]);
    return 0;
}
  • 23
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值