lxhgww的水题 。。 = =像我这种现在只会写水题的人。。。
在求一个点的最长路时:
dis[u] + e[i].w <= dis[v]
dis[v] - dis[u] >= e[i].w
那么这里把每个小朋友看做一个点然后根据不等式求每个点的最长路就好了
没写特判自环就4000多ms也是醉了。。。反正特别这种东西谁爱写谁写吧 = =
#include "cstdio"
#include "iostream"
#include "queue"
#include "cstring"
using namespace std;
const int Nmax = 100005;
const int Mmax = 2 * 100005;
int N, K;
struct ed{
int v, w, next;
}e[Mmax];
int k = 1, head[Nmax];
inline void adde(int u, int v, int w)
{
e[k] = (ed) { v, w, head[u] };
head[u] = k++;
}
queue <int> q;
int dis[Nmax], cnt[Nmax];
bool vis[Nmax];
bool spfa()
{
for (int i = 1; i <= N; ++i) {
dis[i] = vis[i] = 1;
q.push(i);
}
while (q.size()) {
int u = q.front(); q.pop(); vis[u] = 0;
for (int i = head[u]; i; i = e[i].next) {
int v = e[i].v;
if (dis[v] < dis[u] + e[i].w) {
dis[v] = dis[u] + e[i].w;
<span style="white-space:pre"> </span>if (++ cnt[v] > N) return 0;
if (!vis[v]) { vis[v] = 1; q.push(v); }
}
}
}
return 1;
}
int main()
{
cin >> N >> K; int sign, a, b;
while (K --) {
scanf("%d%d%d", &sign, &a, &b);
switch (sign) {
case 1: adde(a, b, 0); adde(b, a, 0); break;
case 2: adde(a, b, 1); break;
case 3: adde(b, a, 0); break;
case 4: adde(b, a, 1); break;
case 5: adde(a, b, 0); break;
}
}
if (spfa()) {
long long ans = 0ll;
for (int i = 1; i <= N; ++i) ans += dis[i];
printf("%lld\n", ans);
} else puts("-1");
return 0;
}