天天看點

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