4097: [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.

Source

Silver

题解:

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

第一次接触分层图最短路。。

其实构图还是挺简单的,把图复制成两份,其中1到k的点向下连边权为0的边。

然后floyd最短路。。

#include<stdio.h>
#include<iostream>
using namespace std;
int n,m,K,Q,i,j,k,x,y,z,cnt,f[405][405];
long long ans;
int main()
{
scanf("%d%d%d%d",&n,&m,&K,&Q);
for(i=1;i<=n<<1;i++)
for(j=1;j<=n<<1;j++) f[i][j]=1e9;
for(i=1;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&z);
f[x][y]=f[x+n][y+n]=z;
}
for(i=1;i<=K;i++) f[i][n+i]=0;
for(k=1;k<=n<<1;k++)
for(i=1;i<=n<<1;i++)
for(j=1;j<=n<<1;j++)
if(f[i][k]+f[k][j]<f[i][j]) f[i][j]=f[i][k]+f[k][j];
while(Q--)
{
scanf("%d%d",&x,&y);
if(f[x][y+n]!=1e9)
{
cnt++;
ans+=f[x][y+n];
}
}
cout<<cnt<<endl<<ans;
return 0;
}

  

bzoj 4097: [Usaco2013 dec]Vacation Planning的更多相关文章

  1. bzoj4097 [Usaco2013 dec]Vacation Planning

    Description Air Bovinia is planning to connect the N farms (1 <= N <= 200) that the cows live ...

  2. [Usaco2013 DEC] Vacation Planning

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=4093 [算法] 对于k个枢纽 , 分别在正向图和反向图上跑dijkstra最短路 , ...

  3. bzoj 4094: [Usaco2013 Dec]Optimal Milking

    4094: [Usaco2013 Dec]Optimal Milking Description Farmer John最近购买了N(1 <= N <= 40000)台挤奶机,编号为1 . ...

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

    最近刷水太多标注一下防止它淹没在silver的水题中……我成为了本题,第一个T掉的人QAQ [题目大意] Bovinia设计了连接N (1 < = N < = 20,000)个农场的航班. ...

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

    [BZOJ4094][Usaco2013 Dec]Optimal Milking Description Farmer John最近购买了N(1 <= N <= 40000)台挤奶机,编号 ...

  6. [bzoj 3048] [Usaco2013 Jan]Cow Lineup

    [bzoj 3048] [Usaco2013 Jan]Cow Lineup Description 给你一个长度为n(1<=n<=100,000)的自然数数列,其中每一个数都小于等于10亿 ...

  7. BZOJ 3389: [Usaco2004 Dec]Cleaning Shifts安排值班

    题目 3389: [Usaco2004 Dec]Cleaning Shifts安排值班 Time Limit: 1 Sec  Memory Limit: 128 MB Description      ...

  8. BZOJ 1692: [Usaco2007 Dec]队列变换( 贪心 )

    数据 n <= 30000 , 然后 O( n² ) 的贪心也过了..... USACO 数据是有多弱啊 = = ( ps : BZOJ 1640 和此题一模一样 , 双倍经验 ) ------ ...

  9. BZOJ 1692: [Usaco2007 Dec]队列变换 [后缀数组 贪心]

    1692: [Usaco2007 Dec]队列变换 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1383  Solved: 582[Submit][St ...

随机推荐

  1. Python os.path.dirname(__file__) os.path.join(str,str)

    Python os.path.dirname(__file__) Python os.path.join(str,str)   (1).当"print os.path.dirname(__f ...

  2. VueJS 轻松支持 JSX 配置

    使用: babel-preset-vue-app TODO

  3. VueJS ElementUI el-table 的 formatter 和 scope template 不能同时存在

    暂时可以通过 在 scope template 中自己处理格式化解决 相关issue: 2548

  4. Supply

    Supplier创建一个Supply Supply有tap或emit方法. 可以这样理解: Supplier创建一个工厂 Supply 用tap创建流水线 emit向流水线上传送加工品进行加厂 my ...

  5. perl6 Socket

    Perl6 中的SOCKET就是相当于Perl5 的 IO::Socket::INET. 官方介绍如下: #下面是客户端multi method new( :$host, :$port, :, :$e ...

  6. 14 - 函数参数检测-inspect模块

    目录 1 python类型注解 2 函数定义的弊端 3 函数文档 4 函数注解 4.1 annotation属性 5 inspect模块 5.1 常用方法 5.2 signature类 5.3 par ...

  7. centos如何设置定时任务

    1.crontab -e 打开任务列表,输入i开始编写面之后按esc退出编写默写,:wq保存退出即可. 2.关于时间格式的定义,,请使用下面的网站 https://crontab.guru/#00_0 ...

  8. 190.Reverse Bits---位运算

    题目链接:https://leetcode.com/problems/reverse-bits/description/ 题目大意:将数值的二进制反转. 法一(借鉴):由于是无符号32位整型,当二进制 ...

  9. 使用extjs做的一个简单grid

    <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding= ...

  10. CGIC简明教程(转摘)

    CGIC简明教程 本系列的目的是演示如何使用C语言的CGI库“CGIC”完成Web开发的各种要求. *********************************     基础知识       1 ...