标签:target std 单源最短路 printf 规划 mem ble using 差分
练习一下那个死了的算法(最近好多东西要用到啊:分数规划、差分约束...)
#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, s;
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)
{
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%d", &n, &m, &s);
int u, v, w;
for(int i = 1; i <= m; i++)
{
scanf("%d%d%d", &u, &v, &w);
add(u, v, w);
}
if(!spfa(s))
{
printf("This graph has got wrong!");
return 0;
}
for(int i = 1; i <= n; i++)
{
printf("%d ", (dis[i] == 0x3f3f3f3f ? 2147483647 : dis[i]));
}
return 0;
}
标签:target std 单源最短路 printf 规划 mem ble using 差分
原文地址:https://www.cnblogs.com/fanypcd/p/14940618.html