标签:lang front std href return first space using include
差分约束模板题。
#include<bits/stdc++.h>
using namespace std;
#define N 1000005
int first[N], Next[N], to[N], w[N], tot;
int dis[N], vis[N], cnt[N], n, m;
void add(int x, int y, int z)
{
Next[++tot] = first[x];
first[x] = tot;
to[tot] = y;
w[tot] = z;
return;
}
int spfa(int x)
{
deque<int> q;
memset(dis, 0x3f, sizeof(dis));
memset(vis, 0, sizeof(vis));
memset(cnt, 0, sizeof(cnt));
dis[x] = 0;
vis[x] = 1;
q.push_front(x);
while(!q.empty())
{
int u = q.front();
q.pop_front();
vis[u] = 0;
for(int i = first[u]; i; i = Next[i])
{
int v = to[i];
if(dis[u] + w[i] < dis[v])
{
dis[v] = dis[u] + w[i];
if(!vis[v])
{
if(++cnt[v] >= n + 1)
{
return 0;
}
vis[v] = 1;
if(q.empty() || dis[v] < dis[q.front()])
{
q.push_front(v);
}
else
{
q.push_back(v);
}
}
}
}
}
return 1;
}
int main()
{
scanf("%d%d", &n, &m);
int u, v, w;
for(int i = 1; i <= m; i++)
{
scanf("%d%d%d", &u, &v, &w);
add(v, u, w);
}
for(int i = 1; i <= n; i++)
{
add(0, i, 0);
}
if(!spfa(0))
{
printf("NO");
return 0;
}
for(int i = 1; i <= n; i++)
{
printf("%d ", dis[i]);
}
return 0;
}
标签:lang front std href return first space using include
原文地址:https://www.cnblogs.com/fanypcd/p/14940620.html