zoukankan      html  css  js  c++  java
  • poj_3159Candies

    Candies
    Time Limit: 1500MS   Memory Limit: 131072K
    Total Submissions: 19969   Accepted: 5263

    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 through N. snoopy and flymouse were always numbered 1 and N. Then follow M lines each holding three integers ABand 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
    #include <iostream>
    #include <queue>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    #pragma warning(disable : 4996)
    const int MAXN = 300005;
    const __int64 INF = 0x7FFFFFFF;
    
    typedef struct ENode
    {
    	int v, w;
    	int next;
    }ENode;
    
    struct node
    {
    	int u;
    	__int64 dis;
    	bool operator < (const node &a) const
    	{
    		return dis > a.dis;
    	}
    };
    
    bool vis[MAXN];
    int n, m, first[MAXN]; //n点数m边数
    __int64 dis[MAXN];
    ENode edge[MAXN];
    priority_queue<node>Q; //node类型的优先队列
    
    void dijkstra()
    {
    	for (int i = 1; i <= n; i++)
    	{
    		vis[i] = false;
    		dis[i] = INF;
    	}
    	dis[1] = 0; 
    	node pre, next;
    	pre.u = 1;
    	pre.dis = 0;
    	Q.push(pre);
    	while(!Q.empty())
    	{
    		pre = Q.top();
    		Q.pop();
    		if(vis[pre.u])//除去同时进几次的
    		{
    			continue;
    		}
    		vis[pre.u] = true;
    		dis[pre.u] = pre.dis;
    		for(int i = first[pre.u]; i != 0; i = edge[i].next)
    		{
    			next.u = edge[i].v;
    			if(!vis[next.u] && dis[edge[i].v] > dis[pre.u] + edge[i].w)
    			{
    				next.dis = pre.dis + edge[i].w;
    				Q.push(next);
    			}
    		}
    	}
    }
    
    int main()
    {
    	freopen("in.txt", "r", stdin);
    	int u, v, w;
    	while (scanf("%d %d", &n, &m) != EOF)
    	{
    		memset(first, 0, sizeof(first));
    		for(int i = 1; i <= m; i++)
    		{
    			scanf("%d %d %d", &u, &v, &w);
    			edge[i].v = v;
    			edge[i].w = w;
    			edge[i].next = first[u];
    			first[u] = i;
    		}
    		dijkstra();
    		printf("%I64d\n", dis[n]);
    	}
    	return 0;
    }



  • 相关阅读:
    SiteMesh入门(1-1)SiteMesh是什么?
    接口和抽象类有什么区别
    StringUtils工具类常用方法汇总(判空、转换、移除、替换、反转)
    StringUtils工具类常用方法汇总(截取、去除空白、包含、查询索引)
    加密方法与HTTPS 原理详解
    String.split()与StringUtils.split()
    自动生成注释
    linux下安装与部署redis
    mybatis批量保存的两种方式(高效插入)
    pagehelper的使用
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/5835031.html
Copyright © 2011-2022 走看看