#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <cstdlib>
using namespace std;
const int INF = 1000000005;
const int MAXN = 1005;
struct edge {
int to, wei;
};
int n, ml, md;
vector<edge> adj[MAXN];
int dis[MAXN], vis[MAXN], cnt[MAXN];
bool flag = true;
void SPFA(int st) {
for (int i = 1; i <= n; i ++)
dis[i] = INF;
memset(vis, false, sizeof(vis));
memset(cnt, 0, sizeof(cnt));
queue<int> q;
q.push(st);
vis[st] = true;
dis[st] = 0;
cnt[st] = 1;
while (!q.empty()) {
int u = q.front();
q.pop();
vis[u] = false;
for (int i = 0; i < adj[u].size(); i ++) {
int v = adj[u][i].to;
int w = adj[u][i].wei;
if (dis[v] > dis[u] + w) {
dis[v] = dis[u] + w;
if (!vis[v]) {
if (cnt[v] > n) {
flag = false;
return;
}
vis[v] = true;
q.push(v);
cnt[v] ++;
}
}
}
}
return;
}
int main() {
scanf("%d%d%d", &n, &ml, &md);
while (ml --) {
int a, b, di;
scanf("%d%d%d", &a, &b, &di);
adj[a].push_back(edge{b, di});
}
while (md --) {
int a, b, di;
scanf("%d%d%d", &a, &b, &di);
adj[b].push_back(edge{a, -di});
}
for (int i = 1; i < n; i ++)
adj[i + 1].push_back(edge{i, 0});
for (int i = 1; i <= n; i ++)
adj[0].push_back(edge{i, 0});
SPFA(0);
if (!flag) {
puts("-1");
return 0;
}
SPFA(1);
if (dis[n] >= INF) {
puts("-2");
return 0;
}
printf("%d\n", dis[n]);
return 0;
}
一本通1512:排队布局
最新推荐文章于 2024-11-05 17:16:24 发布