POJ 3159 差分约束

思路:
很普通很普通的一道差分约束。。 就是Dijkstra的时候要进行堆优化

// by SiriusRen
#include <queue>
#include <cstdio>
#define N 150050
using namespace std;
int n,m,xx,yy,zz,tot=1;
int first[30050],next[N],w[N],v[N],dis[30050];
bool vis[30050];
struct node{int at,weight;}jy,temp;
priority_queue<node>pq;
bool operator <(node a,node b){return a.weight>b.weight;}
void add(int x,int y,int z){
    w[tot]=z;v[tot]=y;
    next[tot]=first[x];first[x]=tot++;
}
void Dijkstra(){
    jy.at=1;jy.weight=0;
    pq.push(jy);
    while(!pq.empty()){
        jy=pq.top(),pq.pop();
        if(vis[jy.at])continue;
        vis[jy.at]=1;dis[jy.at]=jy.weight;
        for(int i=first[jy.at];i;i=next[i]){
            if(vis[v[i]])continue;
            temp.at=v[i],temp.weight=jy.weight+w[i];
            pq.push(temp);
        }
    }
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)scanf("%d%d%d",&xx,&yy,&zz),add(xx,yy,zz);
    Dijkstra();
    printf("%d\n",dis[n]);
}

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值