【bzoj4061】[Cerc2012]Farm and factory
来源:互联网 发布:推荐手机支架 知乎 编辑:程序博客网 时间:2024/06/03 16:49
这题不错
反正我想了一天是没想出来hhhh
后来看了糖老师的题解 恍然大悟
设
设新加的一个点为x
若要使在新图中最短路不经过x,应要满足
这个还是能想得到的
得到了这个以后,因为要使
如果把每个
于是就乱搞咯2333
用dijkstra,时间复杂度
#include <bits/stdc++.h>using namespace std;#define rep(i,a,b) for(int i=a;i<=b;i++)#define per(i,a,b) for(int i=a;i>=b;i--)#define fore(i,u) for(int i=head[u];i;i=nxt[i])#define cr(x) memset(x , 0 , sizeof x)#define pii pair<ll , int>#define mp make_pair#define maxn 100003#define maxm 600007inline int rd() { char c = getchar(); while (!isdigit(c)) c = getchar() ; int x = c - '0'; while (isdigit(c = getchar())) x = x * 10 + c - '0'; return x;}typedef long long ll;typedef int arr_int[maxn];typedef ll arr_ll [maxn];typedef int adj[maxm];const ll inf = 1e12;arr_int head , vis;arr_ll dis[2] , x , y;adj to , val , nxt;int n , m , ett;priority_queue<pii , vector<pii> , greater<pii> > Q;inline void ins(int u , int v , int w) { to[++ ett] = v , val[ett] = w , nxt[ett] = head[u] , head[u] = ett;}void input() { n = rd() , m = rd(); cr(head) , ett = 0; rep (i , 1 , m) { int u = rd() , v = rd() , w = rd(); ins(u , v , w) , ins(v , u , w); }}void dijk(int s) { int c = s - 1; rep (i , 1 , n) dis[c][i] = inf; cr(vis); dis[c][s] = 0; Q.push(mp(0 , s)); while (!Q.empty()) { int u = Q.top().second ; Q.pop(); if (vis[u]) continue; vis[u] = 1; fore (i , u) { int v = to[i] , w = val[i]; if (dis[c][v] > dis[c][u] + w) { dis[c][v] = dis[c][u] + w; Q.push(mp(dis[c][v] , v)); } } }}void solve() { dijk(1) , dijk(2); rep (i , 1 , n) { x[i] = dis[0][i] + dis[1][i]; y[i] = dis[0][i] - dis[1][i]; } sort(x + 1 , x + n + 1) , sort(y + 1 , y + n + 1); ll nx = x[(n + 1) / 2] , ny = y[(n + 1) / 2]; double ans = 0; rep (i , 1 , n) ans += abs(x[i] - nx) + abs(y[i] - ny); printf("%.12lf\n" , ans / (2.0 * n));}int main() { #ifndef ONLINE_JUDGE freopen("data.txt" , "r" , stdin); #endif int T = rd(); rep (i , 1 , T) { input(); solve(); } return 0;}
0 0
- 【bzoj4061】[Cerc2012]Farm and factory
- 【bzoj4061: [Cerc2012]Farm and factory】
- 【bzoj4064】[Cerc2012]The Dragon and the knights
- 【挖坑】CERC2012
- Understanding Factory Method and Abstract Factory Patterns
- Factory mehod and Abstract fatory
- [HDU 5408] CRB and Farm (几何+凸包)
- [CSU 1915 John and his farm]树形DP+LCA
- 【bzoj4062】[Cerc2012]Jewel heist
- 【bzoj4057】[Cerc2012]Kingdoms
- 【CERC2012】【BZOJ4063】Darts
- 【CERC2012】【BZOJ4057】Kingdoms
- 【CERC2012】【BZOJ4062】Jewel heist
- 【bzoj3169: [Cerc2012]Conservation】
- Understanding and Implementing Factory Pattern in C++
- Difference IoC container and Factory pattern
- JS object factory and inherit sample
- angularJs-The factory and service of angularJs
- 窗口可视区和其他一些参数
- jQuery Basics
- 排序
- OpenVc判断矩形是否相交算法
- AndroidStudio中方法注释模板快捷键的设置
- 【bzoj4061】[Cerc2012]Farm and factory
- JdbcTemplate学习笔记
- 常用的图片格式
- Struts2框架的搭建
- 设计模式之合成模式(Java语言描述)
- 如何用rpm查看package的架构
- 获取java线程中信息的方法
- Microsoft Dom xml写入与读取操作笔记
- 观《糖脂大战》总结