码迷,mamicode.com
首页 > 其他好文 > 详细

月亮之眼

时间:2016-02-14 22:15:36      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

#include<cstdio>//https://vijos.org/p/1540

#include<cstring>
#include<iostream>
using namespace std;
int n,m;
int h[10000];
int father[10000];
int found(int);
void show(void)
{
for(int i=1;i<=n;i++)
{
found(i);
}
int minl=0x7f7f7f;
int count=0;
for(int i=1;i<=n;i++)
{
if(h[i]<minl)
{
minl=h[i];
count=i;
}
}
if(minl<0)
{
for(int i=1;i<=n;i++)
{
h[i]-=minl;
printf("%d\n",h[i]);
}
}
else
for(int i=1;i<=n;i++)
{
printf("%d\n",h[i]);
}
}
int found(int x)
{
if(father[x]==x) return x;
else
{
int k=father[x];
father[x]=found(father[x]);
h[x]+=h[k];
return father[x];
}
}
void add(int x,int y,int z)
{
h[father[y]]=h[x]+z-h[y];
father[father[y]]=father[x];
}
void init()
{
int x,y,z;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
father[i]=i;
h[i]=0;
}
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&z);
if(found(x)==found(y))
{
if(h[y]-h[x]!=z)
{
printf("-1");
return;
}
}
else
{
add(x,y,z);
}
}
show();
}
int main()
{
init();
return 0;
}

月亮之眼

标签:

原文地址:http://www.cnblogs.com/lutongxi/p/5189644.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!