[2016-04-08] [POJ] [3159] [Candies]

Source: Internet
Author: User

    • Time: 2016-04-08-18:05:09
    • Title number: [2016-04-08][poj][3159][candies]
    • The main topic: N Children, m information, each message A, B, C, said B children get sugar can not be more than a child C, ask N children than 1 children can more than the maximum number of
    • Analysis: Direct is to ask for 1 to n the shortest path (if not the minimum, then the other side, there is always a situation can not meet the most short-circuit this side of the situation,), run directly once Dijkstra
    • Problem encountered: Using ' vector ' to save the graph tle, changed to an array implementation of the adjacency table is a
  
 
  1. #include <queue>
  2. #include <algorithm>
  3. #include <cstring>
  4. #include <cstdio>
  5. using namespace std;
  6. const int maxn = 3 * 1E4 + 10;
  7. const int maxe = 15 * 1e4 + 10
  8. int mhead[maxn],nxt[maxe],mend[maxe],mcost[maxe],q;
  9. int vis[maxn],d[maxn];
  10. inline void addedge(int from,int to,int c){
  11. mend[q] = to;
  12. nxt[q] = mhead[from];
  13. mcost[q] = c;
  14. mhead[from] = q++;
  15. }
  16. inline void ini(){
  17. memset(mhead,-1,sizeof(mhead));
  18. q = 2;
  19. }
  20. struct mNode{
  21. int u,c;
  22. Mnode ( int _u = 0 int _c = 0 ): u ( _u c ( _c
  23. bool operator < (const mNode & a)const{
  24. return c > a.c;
  25. }
  26. };
  27. void Dijkstra(int s){
  28. memset(vis,0,sizeof(vis));
  29. memset(d,0x3f,sizeof(d));
  30. priority_queue<mNode> q;
  31. d[s] = 0;
  32. q.push(mNode(s,0));
  33. mNode tmp;
  34. while(!q.empty()){
  35. tmp = q.top();
  36. q.pop();
  37. int u = tmp.u;
  38. if(vis[u]) continue;
  39. vis[u] = 1;
  40. for(int e = mhead[u];~e;e = nxt[e]){
  41. int v = mend [ e ;
  42. if (! vis [ v ] && D [ v ] > D [ u + mcost [ e
  43. d[v] = d[u] + mcost[e];
  44. q.push(mNode(v,d[v]));
  45. }
  46. }
  47. }
  48. }
  49. int main(){
  50. int n,m,a,b,c;
  51. ini();
  52. scanf("%d%d",&n,&m);
  53. for(int i = 0 ; i < m ; ++i){
  54. scanf("%d%d%d",&a,&b,&c);
  55. addedge(a,b,c);
  56. }
  57. Dijkstra(1);
  58. printf("%d\n",d[n]);
  59. return 0;
  60. }


From for notes (Wiz)

[2016-04-08] [POJ] [3159] [Candies]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.