洛谷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)个单向航行请求。第i个航行请求是从农场a_i到农场 b_i,航行必须经过至少一个枢纽农场(可以是起点或者终点农场),因此可能会多次经过某些农场。
请计算可行航行请求的数量,及完成所有可行请求的总费用。
输入输出格式
输入格式:
-
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
输出格式:
-
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.
输入输出样例
输入样例#1:3 3 1 3 3 1 10 1 3 10 1 2 7 3 2 2 3 1 2输出样例#1:
2 24
说明
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.
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.
floyd求出最短路,对于每个询问枚举枢纽找最小花费。
/*by SilverN*/ #include<iostream> #include<algorithm> #include<cstring> #include<cstdio> #include<cmath> using namespace std; const int mxn=300; int read(){ int x=0,f=1;char ch=getchar(); while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int mp[mxn][mxn]; int n,m,k,Q; int main(){ memset(mp,0x3f,sizeof mp); n=read();m=read();k=read();Q=read(); int i,j; int u,v,d; for(i=1;i<=m;i++){ u=read();v=read();d=read(); mp[u][v]=min(mp[u][v],d); } for(int e=1;e<=n;e++) for(i=1;i<=n;i++) for(j=1;j<=n;j++){ if(i==j)mp[i][j]=0; else mp[i][j]=min(mp[i][j],mp[i][e]+mp[e][j]); } long long ans=0;int cnt=0; while(Q--){ u=read();v=read(); d=0x3f3f3f3f; for(i=1;i<=k;i++){ d=min(d,mp[u][i]+mp[i][v]); } if(d<0x3f3f3f3f){ cnt++; ans+=d; } } printf("%d\n%lld\n",cnt,ans); return 0; }
- 洛谷 P3094 [USACO13DEC]假期计划Vacation Planning
- [USACO13DEC]假期计划(黄金)Vacation Planning (gold)
- [USACO13DEC]假期计划(黄金)Vacation Planning (gold)
- [USACO13DEC]假期计划(黄金)Vacation Planning (gold)
- 【Floyd(并非水题orz)】BZOJ4093-[Usaco2013 Dec]Vacation Planning
- 洛谷 3097 USACO13DEC 最优挤奶 Optimal Milking
- 【USACO13DEC】洛谷3093 Milk Scheduling
- 洛谷 P3093 [USACO13DEC]牛奶调度Milk Scheduling
- 洛谷 3093 [USACO13DEC]牛奶调度Milk Scheduling
- bzoj千题计划161:bzoj1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果
- [USACO13DEC]最优挤奶Optimal Milking(zkw写法)
- (复习)基础算法--搜索--深入训练(USACO-Feb08、WOW模拟赛Day2-T4、USACO-Dec13、CTSC-1999)
- 【洛谷 3093】[USACO13DEC]牛奶调度Milk Scheduling
- [USACO13DEC] Optimal Milking
- 洛谷P3093 [USACO13DEC]牛奶调度Milk Scheduling(贪心)
- 【USACO13DEC】洛谷3094 Vacation Planning
- 洛谷P3093 [USACO13DEC]牛奶调度Milk Scheduling
- P3093 [USACO13DEC]牛奶调度Milk Scheduling
- 洛谷P3097 - [USACO13DEC]最优挤奶Optimal Milking
- 洛谷P3097 [USACO13DEC]最优挤奶Optimal Milking【线段树】