poj 3159 查分约束 +dij+heap+前向星存图

n表示有n个同学,m表示m组数据

u,v,w,表示同学u要求同学v的糖果数不能多于他超过w个

输出:同学n和同学1的他糖果数最多相差几个??

 

关于查分约束 :https://blog.csdn.net/my_sunshine26/article/details/72849441

与最短路模型的关系 之后就是个最短路的求解

 

之前一直用vector存图,但是超时。。。只好换个前向星的方法

#include<cstdio>
#include<iostream>
#include<queue>
#include<cstring>
#include<cmath>
using namespace std;

#define ll long long
#define pb push_back
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair

const int N  =3e4+4;
const int M  =1e5+5e4+3;
const int INF = 1E9+3;

struct edge{
    int to,cost;
    int next;
};
int d[N];
edge es[M];
int head[N];
int n,m;
int k;
void addedge(int u,int v,int c){
    es[k].to =v;
    es[k].cost =c;
    es[k].next = head[u];
    head[u] = k;
    k++;
}

void dij(){

    for(int i=1;i<=n;++i)d[i]=INF;

        priority_queue< pii , vector <pii>  ,greater<pii >  >Q;
    //val  ind
        d[1]=0;
        Q.push ( mp (0,1));

        while(!Q.empty()){
            pii tmp = Q.top();Q.pop();
            int v = tmp.se;int val = tmp.fi;
            if(d[v]<val)continue;

            for(int i = head[v];i!=-1; i = es[i].next){
                edge e = es[i];
                if(d[e.to]> d[v]+e.cost){
                    d[e.to ] = d[v]+e.cost;
                    Q.push(mp(d[e.to],e.to));
                }
            }
        }
}

int main(){

    while(scanf("%d %d",&n,&m)!=EOF){

        int a,b,c;
        k  =0 ;
        memset(head,-1,sizeof(head));

        while(m--){
            scanf("%d %d %d",&a,&b,&c);
            //x[b]-x[a]<=c
            addedge(a,b,c);
        }
        dij();
        cout<<d[n]<<endl;
    }



    return 0;
}

 

posted on 2018-10-08 20:20  Helpp  阅读(169)  评论(0编辑  收藏  举报

导航