时间限制:10000ms
单点时限:1000ms
内存限制:256MB
描述
Little Hi and Little Ho are playing a construction simulation game. They build N cities (numbered from 1 to N) in the game and connect them by N-1 highways. It is guaranteed that each pair of cities are connected by the highways directly or indirectly.
The game has a very important value called Total Highway Distance (THD) which is the total distances of all pairs of cities. Suppose there are 3 cities and 2 highways. The highway between City 1 and City 2 is 200 miles and the highway between City 2 and City 3 is 300 miles. So the THD is 1000(200 + 500 + 300) miles because the distances between City 1 and City 2, City 1 and City 3, City 2 and City 3 are 200 miles, 500 miles and 300 miles respectively.
During the game Little Hi and Little Ho may change the length of some highways. They want to know the latest THD. Can you help them?
输入
Line 1: two integers N and M.
Line 2 .. N: three integers u, v, k indicating there is a highway of k miles between city u and city v.
Line N+1 .. N+M: each line describes an operation, either changing the length of a highway or querying the current THD. It is in one of the following format.
EDIT i j k, indicating change the length of the highway between city i and city j to k miles.
QUERY, for querying the THD.
For 30% of the data: 2<=N<=100, 1<=M<=20
For 60% of the data: 2<=N<=2000, 1<=M<=20
For 100% of the data: 2<=N<=100,000, 1<=M<=50,000, 1 <= u, v <= N, 0 <= k <= 1000.
输出
For each QUERY operation output one line containing the corresponding THD.
样例输入
3 5
1 2 2
2 3 3
QUERY
EDIT 1 2 4
QUERY
EDIT 2 3 2
QUERY
样例输出
10
14
12
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;
int comp_thd(vector< vector<int> > vv){
int n = vv.size();
int i,j,k;
for(k =0; k<n; ++k){
{for(i = 0; i<n; ++i)
for(j = 0; j <n; ++j)
vv[i][j]=min(vv[i][j],vv[i][k]+vv[k][j]);}
}
int res = 0;
for(i =0; i <n; ++i)
for(j = i+1;j <n; ++j)
res+=vv[i][j];
return res;
}
int main(){
int N,M,u,v,k;
cin>>N>>M;
vector< vector<int> > vv(N, vector<int>(N,2000));
for(int i =0; i<N-1; ++i){
cin>>u>>v>>k;
vv[u-1][v-1]=k;
vv[v-1][u-1]=k;
vv[i][i]=0;
}
vv[N-1][N-1]=0;
int THD;
string s;
for(int j = 0;j<M; ++j){
cin>>s;
if(s=="QUERY"){
THD=comp_thd(vv);
cout<<THD<<endl;
}
else{
cin>>u>>v>>k;
vv[u-1][v-1]=k;
vv[v-1][u-1]=k;
//THD+=((new_k-vv[u-1][v-1])*u*(N-v+1));
}
}
return 0;
}