题目描述
有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.
先用floyd求出任意两点最短路,然后所有不是枢纽的结点的作用就发挥完了。接下来只要枚举枢纽作为中间节点取最小值。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define LL long long
int f[210][210],n,s[10010],t[10010];
int main()
{
int i,j,k,l,m,n,p,q,x,y,z,ans=0,mn;
LL tot=0;
scanf("%d%d%d%d",&n,&m,&l,&q);
memset(f,0x3f,sizeof(f));
for (i=1;i<=n;i++)
f[i][i]=0;
while (m--)
{
scanf("%d%d%d",&x,&y,&z);
f[x][y]=z;
}
for (k=1;k<=n;k++)
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
f[i][j]=min(f[i][j],f[i][k]+f[k][j]);
while (q--)
{
scanf("%d%d",&x,&y);
mn=0x3f3f3f3f;
for (i=1;i<=l;i++)
mn=min(mn,f[x][i]+f[i][y]);
if (mn<0x3f3f3f3f)
{
ans++;
tot+=mn;
}
}
printf("%d\n%lld\n",ans,tot);
}