bzoj4097 [Usaco2013 dec]Vacation Planning

Description

Air Bovinia is planning to connect the N farms (1 <= N <= 200) that the cows live on. As with any airline, K of these farms (1 <= K <= 100, K <= N) have been selected as hubs. The farms are conveniently numbered 1..N, with farms 1..K being the hubs. Currently there are M (1 <= M <= 10,000) one-way flights connecting these farms. Flight i travels from farm u_i to farm v_i, and costs d_i dollars (1 <= d_i <= 1,000,000). The airline recently received a request for Q (1 <= Q <= 10,000) one-way trips. The ith trip is from farm a_i to farm b_i. In order to get from a_i to b_i, the trip may include any sequence of direct flights (possibly even visiting the same farm multiple times), but it must include at least one hub (which may or may not be be the start or the destination). This requirement may result in there being no valid route from a_i to b_i. For all other trip requests, however, your goal is to help Air Bovinia determine the minimum cost of a valid route.

Input

* Line 1: Four integers: N, M, K, and Q.

* Lines 2..1+M: Line i+1 contains u_i, v_i, and d_i for flight i.

* Lines 2+M..1+M+Q: Line 1+M+i describes the ith trip in terms of a_i and b_i

Output

* Line 1: The number of trips (out of Q) for which a valid route is possible.

* Line 2: The sum, over all trips for which a valid route is possible, of the minimum possible route cost.

Sample Input

3 3 1 3
3 1 10
1 3 10
1 2 7
3 2
2 3
1 2
INPUT DETAILS: There are three farms (numbered 1..3); farm 1 is a hub. There is a $10 flight from farm 3 to farm 1, and so on. We wish to look for trips from farm 3 to farm 2, from 2->3, and from 1->2.

Sample Output

2
24
OUTPUT DETAILS: The trip from 3->2 has only one possible route, of cost 10+7. The trip from 2->3 has no valid route, since there is no flight leaving farm 2. The trip from 1->2 has only one valid route again, of cost 7.
Contest has ended. No further submissions allowed.

题意是n个点m条有向边,求两两之间的最短路,要求路径上必须经过编号1~k的至少一个点

先建完图分层,下层把上面复制一遍,然后1~k的点从上层向下层连边权为0的边,跑floyd

我真是bi了狗了开个200*200的数组RE个不停 还被黄巨大批判一番

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 #define LL long long
 6 #define inf 100000000000
 7 using namespace std;
 8 int n,m,k,q,tot;
 9 long long ans;
10 long long dist[410][410];
11 inline LL read()
12 {
13     LL x=0,f=1;char ch=getchar();
14     while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
15     while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
16     return x*f;
17 }
18 int main()
19 {
20     for(int i=1;i<=400;i++)for(int j=1;j<=400;j++)dist[i][j]=inf;
21     n=read();m=read();k=read();q=read();
22     for(int i=1;i<=m;i++)
23     {
24         int x=read(),y=read();
25         dist[x][y]=dist[x+n][y+n]=read();
26     }
27     for(int i=1;i<=k;i++)dist[i][n+i]=0;
28     for(int l=1;l<=2*n;l++)
29         for (int i=1;i<=2*n;i++)
30             for (int j=1;j<=2*n;j++)
31                 if (dist[i][j]>dist[i][l]+dist[l][j])
32                     dist[i][j]=dist[i][l]+dist[l][j];
33     for (int i=1;i<=q;i++)
34     {
35         int x=read(),y=read();
36         if (dist[x][n+y]>1e10)continue;
37         tot++;ans+=dist[x][n+y];
38     }
39     printf("%d\n%lld",tot,ans);
40 }

bzoj4097

时间: 2024-10-13 16:39:33

bzoj4097 [Usaco2013 dec]Vacation Planning的相关文章

bzoj4093: [Usaco2013 Dec]Vacation Planning

bzoj4093: [Usaco2013 Dec]Vacation Planning Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 81  Solved: 38[Submit][Status][Discuss] Description Bovinia设计了连接N (1 < = N < = 20,000)个农场的航班.对于任何航班,指定了其中的k个农场作为枢纽. (1 < = K <= 200 , K < = N). 目前,共有

【Floyd(并非水题orz)】BZOJ4093-[Usaco2013 Dec]Vacation Planning

最近刷水太多标注一下防止它淹没在silver的水题中--我成为了本题,第一个T掉的人QAQ [题目大意] Bovinia设计了连接N (1 < = N < = 20,000)个农场的航班.对于任何航班,指定了其中的k个农场作为枢纽. (1 < = K <= 200 , K < = N). 目前,共有M种单向航班( 1 < = M < = 20,000 ),第i个航班从农场u_i至农场v_i花费d_i ( 1 < = d_i < =10,000 )美元.

【BZOJ4094】[Usaco2013 Dec]Optimal Milking 线段树

[BZOJ4094][Usaco2013 Dec]Optimal Milking Description Farmer John最近购买了N(1 <= N <= 40000)台挤奶机,编号为1 ... N,并排成一行.第i台挤奶机每天能够挤M(i )单位的牛奶 (1 < =M(i) <=100,000).由于机器间距离太近,使得两台相邻的机器不能在同一天使用.Farmer Jo hn可以自由选择不同的机器集合在不同的日子进行挤奶.在D(1 < = D < = 50,00

洛谷P3094 [USACO13DEC]假期计划Vacation Planning

题目描述 有N(1 <= N <= 200)个农场,用1..N编号.航空公司计划在农场间建立航线.对于任意一条航线,选择农场1..K中的农场作为枢纽(1 <= K <= 100, K <= N). 当前共有M (1 <= M <= 10,000)条单向航线连接这些农场,从农场u_i 到农场 v_i, 将花费 d_i美元.(1 <= d_i <= 1,000,000). 航空公司最近收到Q (1 <= Q <= 10,000)个单向航行请求.

BZOJ4095 : [Usaco2013 Dec]The Bessie Shuffle

首先将排列和整个序列以及询问都反过来,问题变成给定一个位置$x$,问它经过若干轮置换后会到达哪个位置. 每次置换之后窗口都会往右滑动一个,因此其实真实置换是$p[i]-1$. 对于每个询问,求出轮数,倍增找到最终位置,注意当中途走到$0$时,说明离开了窗口,应及时终止. 时间复杂度$O((m+q)\log n)$. #include<cstdio> const int N=100010,M=30; int n,m,q,i,j,x,r,k,a[M][N]; inline void read(in

BZOJ 4094 Usaco2013 Dec Optimal Milking 线段树

题目大意:给定n个点排成一排,每个点有一个点权,多次改变某个点的点权并将最大点独立集计入答案,输出最终的答案 开一个线段树,每个点记录四个信息: 区间左端点不选,右端点也不选的最大值 区间左端点选择,右端点不选的最大值 区间左端点不选,右端点选择的最大值 区间左端点选择,右端点也选择的最大值 然后合并时讨论一下就行了 #include <cstdio> #include <cstring> #include <iostream> #include <algorit

[USACO 13DEC]Vacation Planning(gold)

Description Air Bovinia operates flights connecting the N farms that the cows live on (1 <= N <= 20,000). As with any airline, K of these farms have been designated as hubs (1 <= K <= 200, K <= N). Currently, Air Bovinia offers M one-way fl

BZOJ-USACO被虐记

bzoj上的usaco题目还是很好的(我被虐的很惨. 有必要总结整理一下. 3889: [Usaco2015 Jan]Cow Routing 双键值最短路,预处理出代价跑一遍最短路就可以. ★3890: [Usaco2015 Jan]Meeting Time 维护一个小根堆,把边不断地插进去,然后维护一个ans,如果说ans>q.top().t且两个边权都走到n的话,就直接输出答案.否则答案只可能比当前答案还要大. 3891: [Usaco2014 Dec]Piggy Back 做3遍最短路,然

3314: [Usaco2013 Nov]Crowded Cows

3314: [Usaco2013 Nov]Crowded Cows Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 111  Solved: 79[Submit][Status][Discuss] Description Farmer John's N cows (1 <= N <= 50,000) are grazing along a one-dimensional fence. Cow i is standing at location x(