您的位置:首页 > 产品设计 > UI/UE

【POJ3159】【差分系统】【dij+priority_queue】

2015-08-24 10:07 531 查看
Candies

Time Limit: 1500MSMemory Limit: 131072K
Total Submissions: 25849Accepted: 7059
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 A, B and c in order, meaning that kidA 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

POJ Monthly--2006.12.31, Sempr

#include <iostream>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <assert.h>
using namespace std;
    
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define mp push_back

int n,m;
struct Qnode
{
	int idx,dis;
	Qnode(int _idx,int _dis):idx(_idx),dis(_dis) {};
    bool operator< (const Qnode&x) const
	{
		return dis > x.dis;
	}
};
int d[30100];
bool vis[30100];

int H[30100];
int top;
struct Edge
{
	int v,c,next;
}edge[150010];

void addedge(int u,int v,int c)
{
	edge[top].v = v;
	edge[top].c = c;
	edge[top].next = H[u];

	H[u] = top++;
}

int dij(int st)
{
	for(int i=1;i<=n;i++)
	{
		vis[i] = false;
		d[i] = 0x3f3f3f3f;
	}
	d[st] = 0;
	priority_queue<Qnode> pq;
	pq.push(Qnode(st,0));
	for(int i=0;i<n-1;i++)
	{
		while(!pq.empty() && vis[pq.top().idx]) pq.pop();
	
		assert(!pq.empty());
		Qnode cur = pq.top();
		vis[cur.idx] = true;
		pq.pop();
		for(int i=H[cur.idx];i!=-1;i=edge[i].next)
		{
			Edge e = edge[i];
			if(!vis[e.v] && d[e.v] > d[cur.idx] + e.c)
			{
				d[e.v] = d[cur.idx] + e.c;
				pq.push(Qnode(e.v,d[e.v]));
			}
		}

	}
	return d
;
}
int main()
{
    while(scanf("%d%d",&n,&m) != EOF)
	{
		top = 0;
		memset(H,-1,sizeof(H));
		rep(i,0,m)
		{
			int u,v,c;
			scanf("%d%d%d",&u,&v,&c);
			addedge(u,v,c);
		}
		printf("%d\n",dij(1));
	}
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: