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;
    }



  • 相关阅读:
    Asp.Net MVC4入门指南(3):添加一个视图
    Asp.Net MVC4入门指南(2):添加一个控制器
    Asp.Net MVC4入门指南(1): 入门介绍
    .net平台借助第三方推送服务在推送Android,IOS消息(极光推送_V2版本)
    ASP.NET网站实现中英文转换(本地化资源)
    .net 内置对象之Session对象和Session的过期时间
    SQL 单表分页存储过程和单表多字段排序和任意字段分页存储过程
    Js键盘事件全面控制,回车按键事件,键盘对应按键码,按键事件兼容各个浏览器。
    C# WebBrowser控件详解
    html .net 网页,网站标题添图标
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/5835031.html
Copyright © 2011-2022 走看看