ZOJ 3456 Traveler Nobita Minimum Spanning tree

Source: Internet
Author: User

Traveler Nobita Time limit: 2 Seconds Memory Limit: 65536 KB

one day, Nobita used a time machine and went Back to the AD. He found that there are , N  cities in the kingdom he lived. The cities is numbered from 0 to N  -1. Before., there is no roads between any and cities. The kingdom would build one road between the cities at the beginning of each year starting from the AD. There might be duplicated roads between, cities being built by the kingdom. You can assume that building a road takes no time.

at the beginning of every year, after the New Road is built, Nobita would try to make a schedule to travel around all cities within. The travel should both begin at and end at the capital City-city0. Every time Nobita arrived at a city  i , he would spent  T1 i  days in this city, Rega Rdless of how many times he had come to the city. Of course he wouldn ' t need to spend all time in the capital city (that's to say,  T1 0  is alway s 0). and  T2 i  hours is required to pass Road # I . Note that to pass each road, a passport of this road is required. and the kingdom limits that one person can be only with no more than than  N  -1 passports of roads each year.

You is given information about the roads built in M years. Please find the minimum time Nobita needed to complete his traveling schedule.

Input

There is multiple cases. The first line of a test case contains-integers, n (2≤ n ≤200) and m (1≤ m ≤10 The next line contains N integers, indicating t10 ... T1 n -1. (0≤ T1i ≤50) The next M lines, the ith (0≤ i < M) Line of this section contains three I Ntegers, UI, vi, t2i, (0≤ UI, VI < N; 0≤ T2i ≤5000), indicating that in year + i AD., a road would be built between city UI and City vi. t1i and T2i have been described above.

Output

For each case, you should output M lines. For the i-th line, if-Nobita can make a schedule-year + i, output of the minimal days he can finish That's schedule, rounded to both decimal digits. Otherwise output-1. There should is a blank line after each case.

Sample Input
5 60 5 2 5 40 1 10 2 20 3 53 4 22 4 41 2 1
Sample Output
-1-1-121.8319.0019.00
Test instructions: There are n cities, each year to repair a road, a total of M years, attention is from 1000 onwards. Nobita want to walk once a year in N cities, every time from number No. 0, and finally back to No. 0, in each city to T1 days, from one city to another city to T2 hours, pay attention to the time unit is not unified, when judging the year, also pay attention to leap years and common year
Ideas: Kruscal can find the shortest route, for u,v two points between the Benquan, stored in the u,v two cities to stay in the time and walking distance spent time, in No. 0 no need to spend time. There is an optimization in the kruscal process, that is, if two points are already in a set, then the edge of the two points can be deleted, because there has been less than the cost of the edge, so this edge, is useless.
#include <iostream> #include <stdio.h> #include <string> #include <cstring> #include < algorithm> #include <cmath> #include <set> #include <queue> #include <stack> #include <  Vector> #define N 10009using namespace Std;int n,m;int num;int a[n];int fa[n];struct node{int u,v,len;  BOOL operator< (const node &a) Const {return len<a.len;    }};vector<node>ed;int FINDFA (int x) {int r = x, T;   while (R!=fa[r]) r=fa[r];    for (; fa[r]>=0;r=fa[r]);        while (x!=r) {t = fa[x];        Fa[x] = r;    x = t; } return R;} int check (int x) {if (x%400==0 | |    (x%4==0&&x%100))    return 1; return 0;}    void Add (int u,int v,int W) {node e={u,v, (A[u]+a[v]) *24+w*2}; Ed.push_back (e);}    void Uniontwo (int a,int b) {int Aa=findfa (a);    int Bb=findfa (b);    int TMP=FA[AA]+FA[BB];        if (Fa[aa]>fa[bb]) {fa[aa]=bb;    fa[bb]=tmp;        } else {Fa[bb]=aa; Fa[aa]=tmp;    }}int kruscal () {for (int i=1;i<=n;i++) fa[i]=i;    Sort (Ed.begin (), Ed.end ()); int ans=0,cnt=0;//for (vector<node>::iterator it=ed.begin (); It!=ed.end (); it++)//{//Cout<<it-&gt ;u<< "" <<it->v<< "" <<it->len<<endl;//} for (Vector<node>::iterator it=ed.    Begin (); It<ed.end ();)        {int u=it->u;        int v=it->v;        int l=it->len;        int Fu=findfa (u);        int Fv=findfa (v);            if (FINDFA (u)!=findfa (v)) {ans+=l;            cnt++;            it++;            if (FV&GT;FU) Fa[fv]=fu;            else FA[FU]=FV;        Uniontwo (U,V);    } else Ed.erase (it);//There is already a smaller edge for the connection, this edge is useless, can be removed}//cout<< "cnt=" <<cnt<<endl;    if (cnt<n-1) return-1; return ans;}        int main () {while (~scanf ("%d%d", &n,&m)) {num=0;        for (int i=0;i<n;i++) scanf ("%d", &a[i]); Ed.clear ();        int u,v,w;            for (int i=0;i<m;i++) {scanf ("%d%d%d", &u,&v,&w);            Add (U,V,W);                if (i<n-2) {puts ("-1");            Continue            } int x=kruscal ();            cout<< "********" <<endl;            cout<< "x=" <<x<<endl;                if (x==-1) {puts ("-1");            Continue            } int yy;            if (check (1000+i)) yy=366;            else yy=365;            if (yy*24<x) puts ("-1");        else printf ("%.2f\n", x/24.0);    } puts (""); } return 0;}






Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

ZOJ 3456 Traveler Nobita Minimum Spanning tree

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.