zoukankan      html  css  js  c++  java
  • POJ3159 Dijkstra+邻接表+优先队列

    今天学习到了一种新姿势,用邻接表+优先队列优化Dijkstra,这样时间复杂度就由O(N^2+E)变为O(NlogN+E),妈妈再也不用担心我超时了!~(^o^)/

    Candies
    Time Limit: 1500MS   Memory Limit: 131072K
    Total Submissions: 25077   Accepted: 6810

    Description

    During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher brought the kids of flymouse’s class a large bag of candies and had flymouse distribute them. All the kids loved candies very much and often compared the numbers of candies they got with others. A kid A could had the idea that though it might be the case that another kid B was better than him in some aspect and therefore had a reason for deserving more candies than he did, he should never get a certain number of candies fewer than B did no matter how many candies he actually got, otherwise he would feel dissatisfied and go to the head-teacher to complain about flymouse’s biased distribution.

    snoopy shared class with flymouse at that time. flymouse always compared the number of his candies with that of snoopy’s. He wanted to make the difference between the numbers as large as possible while keeping every kid satisfied. Now he had just got another bag of candies from the head-teacher, what was the largest difference he could make out of it?

    Input

    The input contains a single test cases. The test cases starts with a line with two integers N and M not exceeding 30 000 and 150 000 respectively. N is the number of kids in the class and the kids were numbered 1 throughN. snoopy and flymouse were always numbered 1 and N. Then follow M lines each holding three integers AB and c in order, meaning that kid A believed that kid B should never get over c candies more than he did.

    Output

    Output one line with only the largest difference desired. The difference is guaranteed to be finite.

    Sample Input

    2 2
    1 2 5
    2 1 4

    Sample Output

    5
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<queue>
     6 #include<vector>
     7 using namespace std;
     8 #define INF 0x7fffffff
     9 struct node
    10 {
    11     int k,w;
    12 };
    13 bool operator <(const node &t1,const node &t2)
    14 {
    15     return t1.w>t2.w;
    16 }
    17 priority_queue<node>q;
    18 bool vis[30010];
    19 vector<vector<node> >v;
    20 int main()
    21 {
    22     int n,m,i,j,a,b,c;
    23     node p;
    24     while(~scanf("%d%d",&n,&m))
    25     {
    26         v.clear();
    27         v.resize(n+1);
    28         memset(vis,0,sizeof(vis));
    29         for(i=0;i<m;i++)
    30         {
    31             scanf("%d%d%d",&a,&b,&c);
    32             p.k=b;
    33             p.w=c;
    34             v[a].push_back(p);
    35         }
    36         p.k=1;
    37         p.w=0;
    38         q.push(p);
    39         while(!q.empty())
    40         {
    41             p=q.top();
    42             q.pop();
    43             if(vis[p.k])
    44                 continue;
    45             vis[p.k]=true;
    46             if(p.k==n)
    47                 break;
    48             for(i=0;i<v[p.k].size();i++)
    49             {
    50                 node s;
    51                 s.k=v[p.k][i].k;
    52                 if(vis[s.k]) continue;
    53                 s.w=p.w+v[p.k][i].w;
    54                 q.push(s);
    55             }
    56         }
    57         printf("%d
    ",p.w);
    58     }
    59     return 0;
    60 }
    View Code
  • 相关阅读:
    Handlerbars基础笔记
    each()和eq()
    可编辑表格(Editable Table)
    垂直居中vertical-align
    CSS属性的私有前缀
    CSS进阶知识
    CSS预处理器们
    CSS3之伪元素选择器和伪类选择器
    CSS基础复习
    Plan
  • 原文地址:https://www.cnblogs.com/zero-zz/p/4681137.html
Copyright © 2011-2022 走看看