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
  • 相关阅读:
    TDSS 0.0.3 测试版发布,分布式存储系统
    beego中文文档完成+部分新功能和bugfix
    BDD工具Cucumber开发团队扩大且修复了大量Bug
    传微软明年推 Windows 8.2
    试用了Eric4,打算在Eric4中使用Pyqt4写个GUI程序
    玩转Google开源C++单元测试框架Google Test系列(gtest)之八 打造自己的单元测试框架
    代码覆盖率工具大全
    程序员的共鸣 读《卓有成效的程序员》
    玩转Google开源C++单元测试框架Google Test系列(gtest)之二 断言
    玩转Google开源C++单元测试框架Google Test系列(gtest)之五 死亡测试
  • 原文地址:https://www.cnblogs.com/zero-zz/p/4681137.html
Copyright © 2011-2022 走看看