题面
http://darkbzoj.tk/problem/4061
题解
#include<cstdio> #include<cstring> #include<iostream> #include<queue> #include<algorithm> #include<vector> #define ri register int #define N 100050 #define LL long double #include<cmath> using namespace std; int n,m; vector<int> to[N]; vector<LL> len[N]; LL dis[N][2]; bool vis[N]; LL x[N],y[N]; struct node { int x; LL d; bool operator < (const node &rhs) const { return d>rhs.d; } }; void add_edge(int x,int y,LL z) { to[x].push_back(y); len[x].push_back(z); to[y].push_back(x); len[y].push_back(z); } void dij(int s) { priority_queue<node> q; for (ri i=1;i<=n;i++) dis[i][s]=1e18; memset(vis,0,sizeof(vis)); dis[s+1][s]=0; q.push((node){s+1,dis[s+1][s]}); while (!q.empty()) { int x=q.top().x; q.pop(); if (vis[x]) continue; vis[x]=1; for (ri i=0;i<to[x].size();i++) { int y=to[x][i]; if (dis[y][s]>dis[x][s]+len[x][i]) { dis[y][s]=dis[x][s]+len[x][i]; q.push((node){y,dis[y][s]}); } } } } int main() { int T,a,b; LL c; scanf("%d",&T); while (T--) { scanf("%d %d",&n,&m); for (ri i=1;i<=n;i++) to[i].clear(),len[i].clear(); for (ri i=1;i<=m;i++) { scanf("%d %d %Lf",&a,&b,&c); add_edge(a,b,c); } dij(0); dij(1); for (ri i=1;i<=n;i++) x[i]=dis[i][0]+dis[i][1],y[i]=dis[i][0]-dis[i][1]; sort(x+1,x+n+1); sort(y+1,y+n+1); LL sum=0; for (ri i=1;i<=n;i++) { sum+=fabs(x[i]-x[(n+1)/2])+fabs(y[i]-y[(n+1)/2]); } sum/=2*n; printf("%.8Lf ", sum); } return 0; }