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 toN - 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, uivit2i,
(0 ≤ uivi < N; 0 ≤ t2i ≤ 5000), indicating that in year 1000 + i AD., a road will be built between city ui and city vit1i 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年。注意是从1000年開始的。Nobita想要每年走完一次n个城市,每次从0号出发,最后再回到0号,在每一个城市待t1天。从一个城市到还有一个城市要t2小时,注意这里的时间单位不统一。推断年份时,也要注意闰年和平年

思路:kruscal就可以找到最短的路线。对于u,v两个点之间的边权,存的是在u,v两个城市呆的时间和行走路程所用时间。在0号不须要花费时间。

kruscal过程中有一个优化。即假设两个点已经是一个集合中的,那么连接这两点的边就能够删除。由于之前已经有花费更少的边了,所以这条边。就是没用的。
#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#define N 10009
using 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->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>fu)fa[fv]=fu;
else
fa[fu]=fv;
//uniontwo(u,v);
}
else
ed.erase(it);//已经有更小的边用于连接,此边即没什么用。可删除
}
//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;
}


ZOJ 3456 Traveler Nobita 最小生成树的更多相关文章

  1. Traveler Nobita (zoj 3456 最小生成树)

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

  2. 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 ...

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

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

  4. POJ 1861 &amp; ZOJ 1542 Network(最小生成树之Krusal)

    题目链接: PKU:http://poj.org/problem?id=1861 ZJU:http://acm.zju.edu.cn/onlinejudge/showProblem.do?proble ...

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

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

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

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

  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 3204 最小生成树,输出字典序最小的解

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

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

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

随机推荐

  1. ubuntu下安装ftp服务器

    参考文献: 5.4 FTP 服务器 vsftpd - FTP 服务器安装 vsftpd 是可在 Ubuntu 中使用的 FTP 守护程序之一.它在安装.设置和维护方面十分方便.要安装 vsftpd 您 ...

  2. jQuery $('div>ul') $('div ul'

    $('div>ul')是<div>的直接后代里找<ul>: 而$('div ul')是在<div>的所有后代里找<ul>.

  3. Java 中 byte、byte 数组和 int、long 之间的转换

    Java 中 byte 和 int 之间的转换源码: //byte 与 int 的相互转换 public static byte intToByte(int x) { return (byte) x; ...

  4. Windows XP UDF 2.5 补丁,播放蓝光ISO光盘必备

    蓝光光盘的文件系统是UDF2.5,Windows XP及以下的操作系统默认不能支持这个文件系统.当我们在XP系统中使用蓝光光盘或蓝光ISO文件时,就会提示“Windows不能从此盘读取,此盘可能已损坏 ...

  5. Excel部署配置DCOM

    对 Excel进行编程,实际上就是通过 .Net Framework去调用 Excel的 COM组件,所有要在 Web环境下调用 COM组件的时候,都需要对其进行相应的配置. 很多朋友都反映在 Win ...

  6. Windows 和 Linux 平台下的端口转发工具

    原文地址: http://unmi.cc/windows-linux-port-forwarding/ 这里记录一下我曾经使用过的几个端口转发工具,即端口映射.端口重定向,和 NAT 也是差不多的概念 ...

  7. Java 获取客户端IP

    像移动网关一样,iisforward这个ISAPI过滤器也会对request对象进行再包装,附加一些WLS要用的头信息.这种情况下,直接用request.getRemoteAddr()是无法取到真正的 ...

  8. java项目实现流水号自动增长

    项目中有一个规则编号字段,从1开始,编号长度为5位,那么第一条数据编号就是00001. 实现的基本思路就是项目启动时,从数据库获取当前最大值,作为静态变量存储: 业务获取新的编码,考虑并发问题,获取编 ...

  9. 深入理解golang 的栈

    线程栈(thread stacks)介绍 先回顾下linux的内存空间布局   简书_stack02.png 当启动一个C实现的thread时,C标准库会负责分配一块内存作为这个线程的栈.标准库分配这 ...

  10. Java并发编程的艺术(四)——线程的状态

    线程的状态 初始态:NEW 创建一个Thread对象,但还未调用start()启动线程时,线程处于初始态. 运行态:RUNNABLE 在Java中,运行态包括就绪态 和 运行态. 就绪态 该状态下的线 ...