| Time Limit: 1500MS | Memory Limit: 131072K | |
| Total Submissions: 23341 | Accepted: 6293 |
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 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
Source
差分约束的第一题,很简单的建模,但我用spfa超时, 用了 优先队列的dijstra才过的
#include<map>
#include<set>
#include<list>
#include<stack>
#include<vector>
#include<queue>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 30010;
const int M = 150010;
const int inf = 0x3f3f3f3f;
typedef pair<int, int> ll;
struct node
{
int next;
int to;
int weight;
}edge[M];
int head[N];
int dist[N];
int tot;
void addedge(int from, int to, int weight)
{
edge[tot].to = to;
edge[tot].weight = weight;
edge[tot].next = head[from];
head[from] = tot++;
}
void dijkstra(int v0)
{
priority_queue<ll, vector<ll>, greater<ll> >qu;
memset( dist, inf, sizeof(dist) );
dist[v0] = 0;
while( !qu.empty() )
{
qu.pop();
}
qu.push( make_pair(dist[v0], v0) );
while( !qu.empty() )
{
ll tmp = qu.top();
int u = tmp.second;
int dis = tmp.first;
qu.pop();
for(int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if(dist[v] > dis + edge[i].weight)
{
dist[v] = dist[u] + edge[i].weight;
qu.push( make_pair(dist[v], v) );
}
}
}
}
int main()
{
int n, m;
while(~scanf("%d%d", &n, &m))
{
memset( head, -1, sizeof(head) );
tot = 0;
int a, b, c;
for(int i = 1; i <= m; i++)
{
scanf("%d%d%d", &a, &b, &c);
addedge(a, b, c);
}
dijkstra(1);
int maxs = 0;
for(int i = 1; i <= n; i++)
{
maxs = max(dist[i], maxs);
}
printf("%d\n", maxs);
}
return 0;
}
本文解决了一个有趣的算法问题:如何在确保每位小朋友满意的前提下,尽可能拉大两位特定小朋友获得糖果数量之间的差距。通过构建图模型并运用Dijkstra算法求解最大差异。
1817

被折叠的 条评论
为什么被折叠?



