# Traveler Nobita （zoj 3456 最小生成树）

Traveler Nobita

Time Limit: 2 Seconds      Memory Limit: 65536 KB

One day, Nobita used a time machine and went back to 1000 AD. He found that there are N cities in the kingdom he lived. The cities are numbered from 0 to N - 1. Before 1000 AD., there are no roads between any two cities. The kingdom will build one road between two cities at the beginning of each year starting from 1000 AD. There might be duplicated roads between two 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 will try to make a schedule to travel around all cities within that year. The travel should both begin at and end at the capital city - city0. Every time Nobita arrived at a city i, he will spent t1i days in that city, regardless of how many times he had come to the city. Of course he wouldn't need to spend any time in the capital city (that is to say, t10 is always 0). And t2i hours is required to pass road #i. Note that to pass each road, a passport of that road is required. And the kingdom limits that one person can only have no more than N - 1 passports of roads each year.

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

#### Input

There are multiple cases. The first line of a test case contains two integers, N (2 ≤ N ≤ 200) and M (1 ≤ M ≤ 10000). The next line contains N integers, indicating t10 ... t1n - 1. (0 ≤ t1i ≤ 50) The next M lines, the ith (0 ≤ i < M) line of this section contains three integers, ui, vi, t2i, (0 ≤ ui, vi < N; 0 ≤ t2i ≤ 5000), indicating that in year 1000 + i AD., a road will 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 ith line, if Nobita can make a schedule in year 1000 + i, output the minimal days he can finish that schedule, rounded to two decimal digits. Otherwise output -1. There should be a blank line after each case.

#### Sample Input

5 6
0 5 2 5 4
0 1 1
0 2 2
0 3 5
3 4 2
2 4 4
1 2 1


#### Sample Output

-1
-1
-1
21.83
19.00
19.00



#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define maxn 2005
#define MAXN 20025
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b)  for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b)  for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define DBG         pf("Hi\n")
typedef long long ll;
using namespace std;

struct Edge
{
int u,v,len;
bool operator<(const Edge &a)const
{
return len<a.len;
}
};

vector<Edge>edge;
int father[maxn];
int a[maxn];
bool vis[maxn];
int num=0;
int n,m;

void init()
{
num=0;
edge.clear();
}

void addedge(int u,int v,int w,int id)
{
Edge e={u,v,(a[u]+a[v])*24+w*2};
edge.push_back(e);
}

int find_father(int  x)
{
if (x!=father[x])
father[x]=find_father(father[x]);
return father[x];
}

int Kruskal()
{
int i,j;
for (i=0;i<n;i++) father[i]=i;
sort(edge.begin(),edge.end());
int cnt=0,ans=0;
for (vector<Edge>::iterator it = edge.begin();it!=edge.end();)
{
int u=it->u;
int v=it->v;
int l=it->len;
int fu=find_father(u);
int fv=find_father(v);
if (fu!=fv)
{
ans+=l;
it++;
cnt++;
father[fu]=fv;
}
else
edge.erase(it);
//        if (cnt==n-1) break;      //不要break，要把后面无关的边删掉，不然sort会耗时
}
if (cnt<n-1) return -1;
return ans;
}

bool isok(int x)
{
if ((x%4==0&&x%100)||x%400==0) return true;
return false;
}

int main()
{
#ifndef ONLINE_JUDGE
freopen("C:/Users/asus1/Desktop/IN.txt","r",stdin);
#endif
int i,j;
while (~sff(n,m))
{
init();
for (i=0;i<n;i++)
sf(a[i]);
int u,v,w;
for (i=0;i<m;i++)
{
sfff(u,v,w);
int x=Kruskal();
if (x==-1) {
pf("-1\n");
continue;
}
int yy;
if (isok(1000+i)) yy=366;
else yy=365;
if (yy*24<x){
pf("-1\n");
continue;
}
pf("%.2lf\n",x/24.0);
}
pf("\n");
}
return 0;
}


• 本文已收录于以下专栏：

## ZOJ 3456 Traveler Nobita 最小生成树

Traveler Nobita Time Limit: 2 Seconds      Memory Limit: 65536 KB One day, Nobita used a tim...
• wust_ZJX
• 2015年07月29日 10:55
• 602

## ZOJ 3430 Detect the Virus ac自动机 超级精简的转化

Description One day, Nobita found that his computer is extremely slow. After several hours' work,...
• zhou_yujia
• 2015年08月29日 16:32
• 326

## ZOJ 3456

/* * 解法: 首先是建图, 如果边的权值改为它连接的两个节点的时间 + 它表示的路的时间的两倍, * 那么图的最小生成树就是题解. * 然后就是如何求最小...
• RaceBug2010
• 2010年12月28日 13:29
• 735

## Lotus notes traveler测试使用不足汇总

• jackygits
• 2012年08月29日 10:58
• 2378

## Lotus Notes Traveler 收发公司邮件测试成功！

• shichuanbao
• 2008年03月13日 18:28
• 1842

## zoj 2103 Marco Popo the Traveler

第二次做这种题了。 先判连通，然后判欧拉，再找出来符合题意的最短交换次数。。。 WA了数次，WA得我都快没信心了。不过还是找出来好多BUGs，改了之后，顺利AC。。。 先判断连通，用并查集，不连通的...
• zxy_snow
• 2011年02月11日 16:39
• 1121

## 南航3456航班空难乘客子女回忆录

1997年的五月八日夜晚,深圳笼罩在暴风雨之中,9点刚过,从重庆飞来的CZ3456次航班冒着大雨开始了最后的进近··· 连续的3次进近后的结果却是机身解...
• GODFIRE
• 2008年03月08日 15:00
• 7949

## 【ZOJ 2103】 Marco Popo the Traveler

• u011108221
• 2015年04月06日 15:02
• 292

## BZOJ 3456 城市规划 快速傅里叶变换

• PoPoQQQ
• 2015年05月27日 17:08
• 3215

## 南航3456航班空难重建模拟录像

3456航班坠毁过程重建外视录像（FS模拟） 3456航班坠毁驾驶舱重建录像（真实驾驶舱对话录音）  ...
• GODFIRE
• 2008年03月08日 14:56
• 4766

举报原因： 您举报文章：Traveler Nobita （zoj 3456 最小生成树） 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)