1.​​题目链接​​。题目大意:分糖果,其中需要满足约束条件,a要比b多c个,问在满足这样一组条件的情况下,第一个人于最后一个人最多相差多少个糖果。又是典型的差分约束系统,这里有点坑,队列实现的SPFA会超时,改成栈实现的就AC了(T了之后百思不得其解,不知为何,看了聚聚的Blog之后发现栈实现的SPFA可以过,就试了试,发现还真的可以)。

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
#pragma warning(disable:6031)
const int MAXN = 30010;
const int MAXE = 150010;
const int INF = 0x3f3f3f3f;
int head[MAXN];//每个结点的头指针
int vis[MAXN];//在队列标志
int dist[MAXN];
int Q[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++;
}
bool 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;
}
}
}
}
return true;
}
int main()
{
int n, ML;
while (~scanf("%d%d", &n, &ML))
{
tol = 0;
memset(head, -1, sizeof(head));
while (ML--)
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a, b, c);
}
SPFA(1, n);
printf("%d\n", dist[n]);
}
}