天天看点

南邮 OJ 1999 Snoopy

Snoopy

时间限制(普通/Java) :  1000 MS/ 3000 MS          运行内存限制 : 65536 KByte

总提交 : 67            测试通过 : 15 

比赛描述

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?(That means if flymouse has a candies, snoopy has b candises, and we need to calculator max(a-b) ) 

输入

The First line is a integers T indicating there are T test cases.. Each test cases starts with a line with two integers N and M not exceeding 1 000 and 10 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 A, B and c in order, meaning that kid A believed that kid B should never get over c candies more than he did.(1<=A,B<=N,A!=B,1<=c<=1000)

输出

If the largest difference is infinite,then you should output "Infinite" in one line,Or you should Output one line with only the largest difference desired.

样例输入

1

2 2

1 2 5

2 1 4

样例输出

5

提示

null

题目来源

4F

/*
给n个人派糖果,给出m组数据,每组数据包含A,B,c  三个数,
意思是A的糖果数比B少的个数不多于c,即B的糖果数 - A的糖果数<= c 。
最后求n 比 1 最多多多少糖果。
【解题思路】
这是一题典型的差分约束题。不妨将糖果数当作距离,把相差的最大糖果数看成有向边AB的权值,
我们得到 dis[B]-dis[A]<=w(A,B)。看到这里,我们联想到求最短路时的松弛技术,
即if(dis[B]>dis[A]+w(A,B), dis[B]=dis[A]+w(A,B)。
即是满足题中的条件dis[B]-dis[A]<=w(A,B),由于要使dis[B] 最大,
所以这题可以转化为最短路来求。
这题如果用SPFA 算法的话,则需要注意不能用spfa+queue 来求,会TLE ,而是用 spfa + stack

2012-8-17
*/








//SPFA的队列实现会超时,堆栈实现可以。
//没有负环回路判断,堆栈实现SPFA(有时候堆栈确实比较快)
//G++ 516ms

/* AC 78MS
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;

const int MAXN=30010;
const int MAXE=150010;
const int INF=0x3f3f3f3f;
int head[MAXN];//每个结点的头指针
int vis[MAXN];//在队列标志

int Q[MAXN];//堆栈
int dist[MAXN];

struct Edge
{
    int to;
    int v;
    int next;
}edge[MAXE];
int tol;
void add(int a,int b,int v)//加边
{
    edge[tol].to=b;
    edge[tol].v=v;
    edge[tol].next=head[a];
    head[a]=tol++;
}
void SPFA(int start,int n)
{
    int top=0;
    for(int v=1;v<=n;v++)//初始化
    {
        if(v==start)
        {
            Q[top++]=v;
            vis[v]=true;
            dist[v]=0;
        }
        else
        {
            vis[v]=false;
            dist[v]=INF;
        }
    }
    while(top!=0)
    {
        int u=Q[--top];
        vis[u]=false;
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].to;
            if(dist[v]>dist[u]+edge[i].v)
            {
                dist[v]=dist[u]+edge[i].v;
                if(!vis[v])
                {
                    vis[v]=true;
                    Q[top++]=v;
                }
            }
        }
    }
}
int main()
{
   // freopen("in.txt","r",stdin);
  //  freopen("out.txt","w",stdout);
    int n,t;
    int M;
    int a,b,c;
	scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&M);
		tol=0;//加边计数,这个不要忘
        memset(head,-1,sizeof(head));
        while(M--)
        {
            scanf("%d%d%d",&a,&b,&c);
            //b-a<=c
            add(a,b,c);
            //大-小<=c ,有向边(小,大):c
        }
        SPFA(1,n);
		if(dist[n]!=INF){
			printf("%d\n",dist[n]);
		}else{
			printf("Infinite\n");
		}
    }
    return 0;
}
*/




#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;

const int MAXN=1001;
const int MAXE=10001;
const int INF=0x3f3f3f3f;
int head[MAXN];//每个结点的头指针
int vis[MAXN];//在队列标志

int Q[MAXN];//堆栈
int dist[MAXN];

struct Edge
{
    int to;
    int v;
    int next;
}edge[MAXE];
int tol;
void add(int a,int b,int v)//加边
{
    edge[tol].to=b;
    edge[tol].v=v;
    edge[tol].next=head[a];
    head[a]=tol++;
}
void SPFA(int start,int n)
{
    int top=0;
    for(int v=1;v<=n;v++)//初始化
    {
        if(v==start)
        {
            Q[top++]=v;
            vis[v]=true;
            dist[v]=0;
        }
        else
        {
            vis[v]=false;
            dist[v]=INF;
        }
    }
    while(top!=0)
    {
        int u=Q[--top];
        vis[u]=false;
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].to;
            if(dist[v]>dist[u]+edge[i].v)
            {
                dist[v]=dist[u]+edge[i].v;
                if(!vis[v])
                {
                    vis[v]=true;
                    Q[top++]=v;
                }
            }
        }
    }
}
int main()
{
   // freopen("in.txt","r",stdin);
  //  freopen("out.txt","w",stdout);
    int n,t;
    int M;
    int a,b,c;
	scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&M);
		tol=0;//加边计数,这个不要忘
        memset(head,-1,sizeof(head));
        while(M--)
        {
            scanf("%d%d%d",&a,&b,&c);
            //b-a<=c
            add(a,b,c);
            //大-小<=c ,有向边(小,大):c
        }
        SPFA(1,n);
		if(dist[n]!=INF){
			printf("%d\n",dist[n]);
		}else{
			printf("Infinite\n");
		}
    }
    return 0;
}