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. Use JPath but not recursively loop a JObject to modify the values.

    I am dealing with a Json file, I parsed it into jObject, I have another list which flattened the pro ...

  2. Vue 使用自定义组件时报错:Uncaught TypeError: Cannot assign to read only property 'exports' of object '#<Object>'

    自己试做了一下vue的插件 参考element-ui: 写了一个组件 import message from './packages/message/index.js'; const install ...

  3. 面试整理(3)js事件委托

    事件委托主要用于一个父容器下面有很多功能相仿的子容器,这时候就需要将子容器的事件监听交给父容器来做.父容器之所以能够帮子容器监听其原理是事件冒泡,对于子容器的点击在冒泡时会被父容器捕获到,然后用e.t ...

  4. 【译】第三篇 SQL Server代理警报和操作员

    本篇文章是SQL Server代理系列的第三篇,详细内容请参考原文. 正如这一系列的上一篇所述,SQL Server代理作业是由一系列的作业步骤组成,每个步骤由一个独立的类型去执行,除了步骤中执行的工 ...

  5. KEA128单片机启动代码分析

    ;/*****************************************************************************; * @file: startup_SK ...

  6. Perl6多线程3: Promise start / in / await

    创建一个Promise 并自动运行: my $p = Promise.start({say 'Hello, Promise!'}); 如果把代码改成如下, 我们会发现什么也没打印: ;say 'Hel ...

  7. 在字符串S1中删除字符串S2中所包含的字符【转】

    转自:http://www.cnblogs.com/tolimit/p/4202959.html /************************************************** ...

  8. iOS 取消按钮高亮显示方法

    objective-C 第1种方法: 设置按钮的normal 与 highlighted 一样的图片, 不过如果你也需要selected状态下的图片, 就不能这么做, 这样做在取消选中状态的时候就会显 ...

  9. /proc/cpuinfo 文件分析(查看CPU信息)

    /proc/cpuinfo文件分析 根据以下内容,我们则可以很方便的知道当前系统关于CPU.CPU的核数.CPU是否启用超线程等信息. <1>查询系统具有多少个逻辑核:cat /proc/ ...

  10. 在rhel7上搭建centos7的yum源

    1. 再查看现在主机上的yum源,并将它们删除 [root@localhost ~]# rpm -qa|grep yum | xargs rpm -e --nodeps # --nodeps 不管有没 ...