zoukankan      html  css  js  c++  java
  • poj--3159--Candies(简单差分约束)

    Candies
    Time Limit: 1500MS   Memory Limit: 131072K
    Total Submissions: 26888   Accepted: 7398

    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

    Hint

    32-bit signed integer type is capable of doing all arithmetic.

    Source

    数据有点多,不能用cin输入,会超时!!队列也不可以用,也会超时,数组模拟栈就行
    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<queue>
    #include<algorithm>
    using namespace std;
    #define MAXN 30010
    #define MAXM 150000
    #define INF 0x3f3f3f
    int head[MAXN],vis[MAXN],dis[MAXN],Instack[MAXN];
    int n,m,cnt;
    struct node
    {
    	int u,v,val;
    	int next;
    }edge[MAXM];
    void init()
    {
    	memset(head,-1,sizeof(head));
    	cnt=0;
    }
    void add(int u,int v,int val)
    {
    	node E={u,v,val,head[u]};
    	edge[cnt]=E;
    	head[u]=cnt++;
    }
    void getmap()
    {
    	while(m--)
    	{
    		int a,b,c;
    //		cin>>a>>b>>c;
    		scanf("%d%d%d",&a,&b,&c);
    		add(a,b,c);
    	}
    }
    void SPFA()
    {
    	memset(vis,0,sizeof(vis));
    	memset(Instack,0,sizeof(Instack));
    	memset(dis,INF,sizeof(dis));
    	dis[1]=0;
    	vis[1]=1;
    	int top=0;
    	Instack[top++]=1;
    	while(top)
    	{
    		int u=Instack[--top];
    		vis[u]=0;
    		for(int i=head[u];i!=-1;i=edge[i].next)
    		{
    			node E=edge[i];
    			if(dis[E.v]>dis[u]+E.val)
    			{
    				dis[E.v]=dis[u]+E.val;
    				if(!vis[E.v])
    				{
    					vis[E.v]=1;
    					Instack[top++]=E.v;
    				}
    			}
    		}
    	}
    	cout<<dis[n]<<endl;
    }
    int main()
    {
    	while(cin>>n>>m)
    	{
    		init();
    		getmap();
    		SPFA();
    	}
    	return 0;
    }


  • 相关阅读:
    Day22:异常处理、socke基于TCP协议编程
    Day21:面向对象的软件开发、反射、对象的内置方法
    Day20:绑定方法与非绑定办法、多态和多态性
    Day19:继承实现的原理、子类中调用父类的方法、封装
    Day18:类的抽象、类的组合应用
    Day17:类的继承、派生、组合和接口
    Day16:面向对象编程——类和对象
    数据结构
    python爬虫篇之 性能相关
    scrapy-redis
  • 原文地址:https://www.cnblogs.com/playboy307/p/5273535.html
Copyright © 2011-2022 走看看