(File IO): input: toll.in output: toll.out
time limit: 1000 ms space constraints: 262144 KB specific restrictions
Goto ProblemSet
Title Description
with everyone else, Farmer John would rather teach me negative to the cattle world, the world of cattle called off the negative spirit of my great, day and night trying to come up way of making money. In order to make a fortune, he set up a series of rules and regulations so that any cows walking in a farm road tolls should be turned over to Farmer John.
Farm by N (1 <= N <= 250) meadow (numbered 1 to N), and has a M (1 <= M <= 10000) bi-directional road link grass A_j and B_j (1 <= A_j <= N; 1 <= B_j <= N). The cows can reach any departure from any one of a grassy meadow. FJ has provided a toll road is connected over a bidirectional L_j A_j and B_j of (1 <= L_j <= 100,000).
Possible number of roads connecting the same two meadows, but there is no road connecting a meadow grass and this itself. The good news is that most cows from any of a lawn, through a series of paths, you can always reach any other of a meadow.
In addition to greed, we do not know what to say. FJ even in each of the above is also provided with a meadow tolls C_i (1 <= C_i <= 100000). From a grassy meadow to another piece of the cost, it is the result of all road tolls and the maximum tolls plus all the grass passing (including start and end) of.
Hard working cattle who want to investigate what they should choose that path. They want you to write a program that accepts K (1 <= K <= 10,000) problem and the minimum cost per output corresponding to the challenge. The i-th question comprises two numbers and s_i t_i (1 <= s_i <= N; 1 <= t_i <= N;! S_i = t_i), showing the start and end of the lawn.
Consider the following sample image comprising five grass:
from a road to a meadow grass 3 'side of tolls "3," toll point "grassland 2 to 5.
1 come from the grass lawn 4, may come from the grass lawn 13 and then went to the grass lawn 4 5 finally arrived. If so go, need "side tolls" is 2 + 1 + 1 = 4, points need to tolls 4 (grass point 5 tolls maximum), so the total cost is 4 + 4 = 8.
The best route is from 2 to 3 turf grass from the lawn 2, 5 arrived in the grass, grass and finally to 3. So go, tolls for the edge 3 + 1 = 4, 5 points - the toll is a total cost of 5 + 4 = 9.
Input
Line 1: three spaces separated integers: N, M and K
second through N + 1 line: first line i + 1 contains a single integer: C_i
of the first N + 2 to N + M + 1 : line j + N + 1 line contains three integers separated by spaces: A_j, B_j L_j and
the first N + M + 2 inverted first N + M + K + 1: line i + N + M + 1 rows denotes the i th question comprises two spaces separated by an integer and s_i t_i
Outputs
of the first to K line: the i-th row contains a single integer, representing the minimum cost to s_i from the t_i.
Sample input
. 5. 7 2
2
. 5
. 3
. 3
. 4
. 1 2. 3
. 1. 3 2
2. 5. 3
. 5. 3. 1
. 5. 1. 4
2. 4. 3
. 3. 4. 4
. 1. 4
2. 3
Sample output
. 8
. 9
Data range limit
Problem-solving ideas
multi-source shortest path problem, a small range of data, it is clear that you can use
algorithm to process,
since the shortest path relates to the maximum point in the right path, so how fast state transition decision stage, results in the point of maximum path weight is the core consideration of this question. If every right to enumerate point, the complexity is clearly explosion.
We again recall again
algorithm works:
I to j, there are two possibilities: to directly and by means of the intermediate interfaces
, takes a
on the line
we want to look further down thin,
represents the middle of the interface, and
enumeration order is arbitrary?
Manifestly arbitrary! A breakthrough in this: that we can modify the wanton
enumeration order! Therefore, this question will be solved.
We tap weights from small to large renumbering, which is a transit point
representatives is renumbered point,
greater, the greater the right point
we just need to enumerate small to large transit point
, because the
is the enumeration of small to large
we are looking for
shortest path between, we transit point, if only to enumerate
, which explains our current
shortest between the no right to a little more than
great little spot right last
i.e. as
In addition to the path
maximum outer right point,
and then to run can
maximum point on the path is the right
three points right to the midpoint of the maximum value!
Code
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
#include<iomanip>
#include<cmath>
using namespace std;
const int INF=0x3f3f3f3f;
int n,m,K,b[260],p,q,dis[260][260],ans[260][260];
struct c{
int x,y;
}a[260];
bool cmp(const c&l,const c&r){
return l.x<r.x;
}
int main(){
freopen("toll.in","r",stdin);
freopen("toll.out","w",stdout);
scanf("%d%d%d",&n,&m,&K);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i].x);
a[i].y=i;
}
sort(a+1,a+n+1,cmp);
for(int i=1;i<=n;i++)
b[a[i].y]=i;
memset(dis,INF,sizeof(dis));
for(int i=1;i<=n;i++)
dis[i][i]=0;
for(int i=1;i<=m;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
dis[b[u]][b[v]]=dis[b[v]][b[u]]=min(dis[b[u]][b[v]],w);
}
memset(ans,INF,sizeof(ans));
for(int k=1;k<=n;k++)
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
if(i==j)
continue;
dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
if(dis[i][j]==dis[i][k]+dis[k][j])
ans[i][j]=min(ans[i][j],dis[i][j]+max(a[i].x,max(a[j].x,a[k].x)));
}
for(int i=1;i<=K;i++)
{
scanf("%d%d",&p,&q);
printf("%d\n",ans[b[p]][b[q]]);
}
}