poj 3159 Candies(dijstra最佳化非vector寫法)

來源:互聯網
上載者:User

標籤:main   條件   多少   cstring   opera   ace   name   重複   cto   

題目連結:http://poj.org/problem?id=3159

題意:給n個人派糖果,給出m組資料,每組資料包含A,B,c 三個數,意思是A的糖果數比B少的個數不多於c,即B的糖果數 - A的糖果數<= c 。

最後求n比1最多多多少糖果。

可以從條件中的

B-A<=c及B<=A+c最後要達成這個條件就是要當B>A+c時B=A+c即可

所以差不多就是求最短路。這題中還有一些最佳化比如

if(vis[u]) continue;這個避免了u點的重複尋找

 

#include <iostream>#include <string>#include <cstring>#include <vector>#include <queue>#include <cstdio>#define inf 0X3f3f3f3fusing namespace std;const int M = 2e5;int n , m , a , b , c , dis[30010];struct TnT {    int u , v , w , next;}T[M];struct qnode{    int v , c;    qnode(int v , int c):v(v) , c(c){}    bool operator < (const qnode &r) const{        return c > r.c;    }};int head[30010] , e;void add(int u , int v , int w) {    T[e].v = v;    T[e].w = w;    T[e].next = head[u];    head[u] = e++;}bool vis[M];void dij(int s) {    priority_queue<qnode>q;    memset(vis , false , sizeof(vis));    q.push(qnode(s , 0));    dis[s] = 0;    while(!q.empty()) {        int u = q.top().v;        q.pop();        if(vis[u])            continue;        vis[u] = true;        for(int i = head[u] ; i != -1 ; i = T[i].next)  {            int v = T[i].v , w = T[i].w;            if(!vis[v] && dis[v] > dis[u] + w) {                dis[v] = dis[u] + w;                q.push(qnode(v , dis[v]));            }        }    }}int main() {    while(scanf("%d%d" , &n , &m) != EOF) {        e = 0;        for(int i = 1 ; i <= n ; i++) {            dis[i] = inf;            head[i] = -1;        }        for(int i = 1 ; i <= m ; i++) {            scanf("%d%d%d" , &a , &b , &c);            add(a , b , c);        }        dij(1);        printf("%d\n" , dis[n]);    }    return 0;}

poj 3159 Candies(dijstra最佳化非vector寫法)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.