# [Luogu 3275] SCOI2011 糖果

<题目链接>

#include <algorithm>
#include <cstdio>
#include <queue>

#define nullptr NULL

const int MAXN = 100010;

int n, k;
long long ans;

struct Graph
{
struct Edge
{
int to;
long long w;
Edge *next;
Edge(int to, long long w, Edge* next): to(to), w(w), next(next) {}
~Edge(void)
{
if(next != nullptr)
delete next;
}
Graph(int n)
{
}
~Graph(void)
{
for(int i = 1; i <= n; ++i)
}
void AddEdge(int u, int v, long long w)
{
}
void Build(int x, int a, int b)
{
if(x % 2 == 0 && a == b)
{
puts("-1");
exit(0);
}
switch(x)
{
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
}
}
}*G;

namespace DiffConst
{
bool exist[MAXN];
int cnt[MAXN];
long long dist[MAXN];
bool SPFA(int S)
{
std :: queue<int> Q;
Q.push(S);
exist[S] = true;
while(!Q.empty())
{
int u = Q.front(), v;
Q.pop();
exist[u] = false;
for(Graph :: Edge *i = G -> head[u]; i != nullptr; i = i -> next)
if(dist[v = i -> to] < dist[u] + i -> w)
{
if(!exist[v])
{
if(++cnt[v] == n)
return false;
Q.push(v);
exist[v] = 1;
}
dist[v] = dist[u] + i -> w;
}
}
return true;
}
}

int main(void)
{
scanf("%d %d", &n, &k);
G = new Graph(n);
for(int i = 1, x, a, b; i <= k; ++i)
{
scanf("%d %d %d", &x, &a, &b);
G -> Build(x, a, b);
}
for(int i = n; i >= 1; --i)
if(DiffConst :: SPFA(0))
{
for(int i = 1; i <= n; ++i)
ans += DiffConst :: dist[i];
printf("%lld\n", ans);
}
else
puts("-1");
return 0;
}


posted @ 2018-11-03 00:37  Capella  阅读(109)  评论(0编辑  收藏  举报