vijos:p1540 Eye of the Moon

Source: Internet
Author: User

Describe

Jill is an antique shop owner, because she runs the way, the store is booming. Yesterday, Jill accidentally got scattered folk hundreds of years of treasures-the eye of the moon. Jill know that "the Eye of the Moon" is priceless: it is made up of many pearls, craftsmen connect pearls with gold wires, each gold thread connects two pearls, and at the same time, two colors are dyed on each gold thread, half of them silver and half dyed in black. Since Jill from childhood familiar with ancient books, so also know "the Eyes of the moon" The Mysterious legend: "The Eye of the Moon" was originally an ancient temple treasures, originally hung in the temples of a pillar, the entire treasure vertical suspension, all the pearls lined up in a line, and are inlaid in the pillars, and each of the gold lines are taut, and the silver-white end of the gold Line is always on the top of the black one side; however, in the one-month round night, the "Eye of the Moon" suddenly flew out of the column, falling Down, the treasure itself intact, but the monks can no longer be the same as the "Eye of the Moon" embedded in the pillar. Jill looking at this mysterious treasure, memories of childhood read the legend, immediately germination recovery "the eyes of the moon" impulse, but fiddling with a few days still no success.

Now, we need to trouble you to help Jill complete this mission.

You want to design a program for a given "eye of the Moon" for a thorough analysis, and then give this string of treasures hundreds of years ago embedded in the temple pillar of the arrangement. Given the "Eye of the Moon" there are N pearls and P-root gold lines, and all pearls have a sequence number in a certain order: 1, 2 ..., N.

Format input Format

The input data contains a feature description of the "Eye of the Moon":
The first line of the file has two integers n and p, where n denotes the number of pearls in the treasure, and P indicates the number of gold lines in the treasure;
The following P-line describes the Pearl connection condition:
The file i+1 line has three integers, ri1,ri2,li. Wherein RI1 represents the silver of the Gold Line of the first link of the pearl serial number; Ri2 represents the pearl serial number connected to the black end of the gold line of the first I, and Li represents the length of the gold line of the first I.

Output format

Because the pearl size is small, several pearls can be set in one place at a time.

Your output data describes the "eyes of the Moon" in the position of each pearl on the pillar, the output file total N rows:
Line I, an integer s, which represents the distance from the highest position of pearls on the pillar of the pearl labeled I.

Note: If there is no solution then output only one line, containing an integer "1".

Input:

9 9
1 2 3
2 3 5
2 7 1
4 5 4
5 6 1
5 9 1
6 7 1
7 8 3
9 8 4

Output

2
5
10
0
4
5
6
9
5

Train of thought: negative upward, down is positive, BFS 1th Pearl to each pearl distance (Spfa also can), find the minimum distance, is the top of the pearl, set it to K. So the distance of other pearls to Pearl K is d[i]-d[k]

#include <cstdio>#include<cstring>#include<vector>#include<queue>using namespacestd;Const intmaxn=505;Const intinf=0x7f7f7f7f;structedge{intto,w; Edge () {} Edge (intTo,intW) { This->to=to ;  This->w=W; }};vector<Edge>MP[MAXN];intD[MAXN],VIS[MAXN];intCNT[MAXN];intn,m;BOOLBFsintu) {     for(intI=1; i<=n;i++) {D[i]=INF; Vis[i]=0; } Queue<int>que; D[u]=0; Vis[u]=1;    Que.push (U);  while(!Que.empty ()) {        intnow=Que.front (); Que.pop ();  for(intI=0; I<mp[now].size (); i++) {Edge e=Mp[now][i]; intl=d[now]+E.W; if(vis[e.to]&&l!=D[e.to]) {                return false; }            Else if(!Vis[e.to])                {Que.push (e.to); Vis[e.to]=1; D[e.to]=M; }        }            }    return true;}intMain () {scanf ("%d%d",&n,&m);  for(intI=0; i<m;i++)    {        intu,v,w; scanf ("%d%d%d",&u,&v,&W);        Mp[u].push_back (Edge (v,w)); Mp[v].push_back (Edge (U,-W)); }    if(BFS (1))    {        intmn=INF;  for(intI=1; i<=n;i++)        {            if(d[i]<mn) {MN=D[i]; }        }             for(intI=1; i<=n;i++) {D[i]-=mn; }         for(intI=1; i<=n;i++) {printf ("%d", D[i]); if(i!=N) printf ("\ n"); }    }    Elseprintf ("-1\n"); return 0;} 

vijos:p1540 Eye of the Moon

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.