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
题意: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 最小生成树)的更多相关文章
- ZOJ 3456 Traveler Nobita 最小生成树
Traveler Nobita Time Limit: 2 Seconds Memory Limit: 65536 KB One day, Nobita used a time machin ...
- zoj 3204 最小生成树,输出字典序最小的解
注意排序即可 #include<cstdio> #include<iostream> #include<algorithm> #include<cstring ...
- ZOJ 1542 POJ 1861 Network 网络 最小生成树,求最长边,Kruskal算法
题目连接:problemId=542" target="_blank">ZOJ 1542 POJ 1861 Network 网络 Network Time Limi ...
- ZOJ 1203 Swordfish 旗鱼 最小生成树,Kruskal算法
主题链接:problemId=203" target="_blank">ZOJ 1203 Swordfish 旗鱼 Swordfish Time Limit: 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 ...
- ZOJ 1586 QS Network (最小生成树)
QS Network Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Submit Sta ...
- ZOJ 1584:Sunny Cup 2003 - Preliminary Round(最小生成树&&prim)
Sunny Cup 2003 - Preliminary Round April 20th, 12:00 - 17:00 Problem E: QS Network In the planet w-5 ...
- zoj 2966 Build The Electric System 最小生成树
Escape Time II Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/showP ...
- ZOJ 1586 QS Network Kruskal求最小生成树
QS Network Sunny Cup 2003 - Preliminary Round April 20th, 12:00 - 17:00 Problem E: QS Network In the ...
随机推荐
- Mysql学习总结(6)——MySql之ALTER命令用法详细解读
MySql语法中Alter命令的用法,这是一个用法比较多的语法,而且功能还是很强大的. [sql] view plaincopy USE learning;(自己要提前建好) CREATE TABLE ...
- jmeter名词解释之聚合报告
新浪围脖>@o蜗牛快跑o 温馨提示: 1. tps(吞吐量)表征系统性能,系统的好坏能够用这个评估 2. 90%Line是满足需求响应时间的重要指标,假设用户需求说是响应时间不小于5s,那 ...
- 【shell学习】经常使用条件推断-字符,数字,文件
IF 推断 之前也写过简单的shell脚本,也不是转职运维.和系统相关的工作比較少.所以不怎么熟练. 近期因为系统总是出现各种乱七八糟的问题,也没有人来协助.仅仅好自己写shell脚本了,都是些基础的 ...
- servlet学习(1)
1.Servlet是sun公司提供的一门用于开发动态web资源的技术. 2.Servlet在web应用的位置: 3.创建Servlet的三种方式: (1)实现servlet的接口 (2)继承Gener ...
- 智课雅思词汇---三、aud和auto和bene是什么意思
智课雅思词汇---三.aud和auto和bene是什么意思 一.总结 一句话总结:aud:听 auto:自己,self bene:good,well 1.anthropo是什么意思? anthropo ...
- HTTP基础知识整理
http请求由三部分组成,分别是:请求行.消息报头.请求正文 HTTP(超文本传输协议)是一个基于请求与响应模式的.无状态的.应用层的协议,常基于TCP的连接方式,HTTP1.1版本中给出一种持续连接 ...
- Day5费用流
算法 zkw费用流:多路增广,增光D[y]=D[i]+c的边 无源汇上下界最小费用可行流 每次强行增加下界的流量 类似网络流,拆边 原边的费用为c,拆出来的边费用为0 负边和负圈 直接应用 SDOI2 ...
- ifconfig---配置和显示Linux内核中网络接口
ifconfig命令被用于配置和显示Linux内核中网络接口的网络参数.用ifconfig命令配置的网卡信息,在网卡重启后机器重启后,配置就不存在.要想将上述的配置信息永远的存的电脑里,那就要修改网卡 ...
- celery work logging 问题
celery 的日志里只输出日志 不输入标准打印
- 洛谷——P2695 骑士的工作
https://www.luogu.org/problem/show?pid=2695 题目背景 你作为一个村的村长,保卫村庄是理所当然的了.今天,村庄里来了一只恶龙,他有n个头,恶龙到处杀人放火.你 ...