AcWing1170. 排队布局[USACO05]
解题思路
从题目中我们可以看出这样几条约束关系
:
然后又没有一个源点可以遍历所有边
,所以我们还是建立超级源点
整理关系得到
代码
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1010, M = 21010, INF = 0x3f3f3f3f;
int h[N], e[M], w[M], ne[M], idx;
int dist[N], cnt[N], q[N], st[N];
int hh, tt(1), md, ml, n, ring;
void add(int a, int b, int c)
{
e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx ++ ;
}
void spfa(int S)
{
memset(dist, 0x3f, sizeof dist);
memset(st, 0, sizeof st);
memset(cnt, 0, sizeof cnt);
q[tt ++ ] = S, st[S] = 1, dist[S] = 0;
while (hh != tt)
{
int t = q[hh ++ ];
if (hh == N) hh = 0;
st[t] = 0;
for (int i = h[t]; ~i; i = ne[i])
{
int j = e[i];
if (dist[j] > dist[t] + w[i])
{
dist[j] = dist[t] + w[i];
cnt[j] = cnt[t] + 1;
if (cnt[j] >= n) return void (ring = true);
if (!st[j])
{
st[j] = true ;
q[tt ++ ] = j;
if (tt == N) tt = 0;
}
}
}
}
return void (ring = false);
}
int main()
{
scanf("%d%d%d", &n, &ml, &md);
memset(h, -1, sizeof h);
while (ml -- )
{
int a, b, l;
scanf("%d%d%d", &a, &b, &l);
if (a > b) swap(a, b);
add(a, b, l);
}
while (md -- )
{
int a, b, d;
scanf("%d%d%d", &a, &b, &d);
if (a > b) swap(a, b);
add(b, a, -d);
}
for (int i = 1; i < n; i ++ )
add(i + 1, i, 0), add(0, i, 0);
add(0, n, 0);
spfa(0);
if (ring) puts("-1");
else {
spfa(1);
printf("%d\n", dist[n] == INF ? -2 : dist[n]);
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)