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

题意:n个点m条路,開始没有路。每一年修一条路。修完后一个人从0点周游这n个点。问是否能在一年内游玩这n个点,能的话输出最少的天数。输入会告诉每一个点他待的时间和每条路走的时间,他最多仅仅能走n-1条路。

思路:一边加边一边Kruskal,每次Kruskal把没实用的边删掉,另外前n-2年肯定不能完毕。还要注意闰年。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#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);
addedge(u,v,w,i);
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;
}

Traveler Nobita (zoj 3456 最小生成树)的更多相关文章

  1. ZOJ 3456 Traveler Nobita 最小生成树

    Traveler Nobita Time Limit: 2 Seconds      Memory Limit: 65536 KB One day, Nobita used a time machin ...

  2. zoj 3204 最小生成树,输出字典序最小的解

    注意排序即可 #include<cstdio> #include<iostream> #include<algorithm> #include<cstring ...

  3. ZOJ 1542 POJ 1861 Network 网络 最小生成树,求最长边,Kruskal算法

    题目连接:problemId=542" target="_blank">ZOJ 1542 POJ 1861 Network 网络 Network Time Limi ...

  4. ZOJ 1203 Swordfish 旗鱼 最小生成树,Kruskal算法

    主题链接:problemId=203" target="_blank">ZOJ 1203 Swordfish 旗鱼 Swordfish Time Limit: 2 ...

  5. ZOJ - 3204 Connect them 最小生成树

    Connect them ZOJ - 3204 You have n computers numbered from 1 to n and you want to connect them to ma ...

  6. ZOJ 1586 QS Network (最小生成树)

    QS Network Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Sta ...

  7. ZOJ 1584:Sunny Cup 2003 - Preliminary Round(最小生成树&amp;&amp;prim)

    Sunny Cup 2003 - Preliminary Round April 20th, 12:00 - 17:00 Problem E: QS Network In the planet w-5 ...

  8. zoj 2966 Build The Electric System 最小生成树

    Escape Time II Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/showP ...

  9. ZOJ 1586 QS Network Kruskal求最小生成树

    QS Network Sunny Cup 2003 - Preliminary Round April 20th, 12:00 - 17:00 Problem E: QS Network In the ...

随机推荐

  1. whereis---定位指令的二进制程序、源代码文件和man手册页等相关文件的路径。

    whereis命令用来定位指令的二进制程序.源代码文件和man手册页等相关文件的路径. whereis命令只能用于程序名的搜索,而且只搜索二进制文件(参数-b).man说明文件(参数-m)和源代码文件 ...

  2. paste---合并文件的列。

    Linux paste命令用于合并文件的列. paste指令会把每个文件以列对列的方式,一列列地加以合并. 语法 paste [-s][-d <间隔字符>][--help][--versi ...

  3. jquery正则匹配URL地址

    JQuery代码: var regexp = /((http|ftp|https|file):\/\/([\w\-]+\.)+[\w\-]+(\/[\w\u4e00-\u9fa5\-\.\/?\@\% ...

  4. 【Henu ACM Round#17 E】Tree Construction

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 做这题之前先要知道二叉排序树的一个性质. 就是它的中序遍历的结果就是这个数组升序排序. (且每个节点的左边的节点都是比这个节点的值小 ...

  5. HOJ——T 2275 Number sequence

    http://acm.hit.edu.cn/hoj/problem/view?id=2275 Source : SCU Programming Contest 2006 Final   Time li ...

  6. Android应用公布后的统计——百度移动统计的应用

    一个App公布到各个渠道之后,我们须要採集不同渠道的一些信息,比方app在执行过程中产生的一些异常信息,app在各个android版本号的分布,以及各个app版本号的分布,各渠道的用户数,用户忠诚度等 ...

  7. Linux中配置网桥

    使用kvm虚拟机时,有时候需要自己添加网桥供guest使用. 不使用libvirt来管理的话,可以使用以下方法创建网桥并绑定到物理网卡(RHEL6/Fedora已实验): 1.创建网桥配置文件ifcf ...

  8. 从头认识Spring-2.4 基于java的标准注解装配-@Inject(2)-通过set方法或者其它方法注入

    这一章节我们来讨论一下基于java的标准注解装配标签@Inject是如何通过通过set方法或者其它方法注入? 在使用@Inject标签之前.我们须要在pom文件中面增加以下的代码: <depen ...

  9. 8.AXIS1基础

    转自:https://blog.csdn.net/chjttony/article/details/6209998 1.AXIS简介: Axis是Apache组织推出的SOAP引擎,Axis项目是Ap ...

  10. typeof 和 instanceof 的区别

    在JavaScript中我们想得到一个变量的类型,我们一般会用typeof 得到这个类型的 字符串,但是对于引用类型,typeof始终会返回一个"object",在我们js中有十个 ...