https://www.cnblogs.com/31415926535x/p/11440395.html

一道简单的Floyd题,,但是是动态加点求多次有限制的最短路,,感觉这个思想很好,,当然可以直接dp

题意

题目给你一个图,然后对于每一个节点都有一个点权,然后有q次询问,每次询问两点间的最短距离,并且最短路径中不能通过任意一个点权大于等于w的点,(首尾不算),,

思路

一次询问的话,直接最短路乱搞就行了,,但是询问次数很多的时候,就不能每一次建图跑,因为是任意两点的最短路,而且给的图是邻接矩阵 ,所以用Floyed,,,但是怎么处理每一次的询问呢,,一种做法是再加一维,处理出任意的加入前k个点后的最短路,,最后回答询问即可,,,也就是dp的思想,,另一种是询问离线,动态建图跑q次floyed即可,,后面这种思路以前见过但是没套floyed用过,,

代码

离线

#include <bits/stdc++.h>
#define aaa cout<<233<<endl;
#define endl '\n'
#define pb push_back
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
// mt19937 rnd(time(0));
const int inf = 0x3f3f3f3f;//1061109567 > 1e9
const ll linf = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-7;
const double pi = 3.14159265358979;
const int maxn = 2e2 + 5;
const int maxm = 2e4 + 5;
const int mod = 1e9 + 7; int d[maxn][maxn];
struct query
{
int u, v, w;
int ans;
int id;
const bool operator<(const query &q)const
{
return w < q.w;
}
}qry[maxm];
bool cmpid(query a, query b)
{
return a.id < b.id;
}
pair<int, int> r[maxn];
int main()
{
// double pp = clock();
// freopen("233.in", "r", stdin);
// freopen("233.out", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0); int t; cin >> t;
int ca = 1;
while(t--)
{
int n, q; cin >> n >> q;
for(int i = 1; i <= n; ++i)cin >> r[i].first;
for(int i = 1; i <= n; ++i)r[i].second = i;
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
cin >> d[i][j];
for(int i = 1; i <= q; ++i)cin >> qry[i].u >> qry[i].v >> qry[i].w;
for(int i = 1; i <= q; ++i)qry[i].id = i;
sort(qry + 1, qry + 1 + q);
sort(r + 1, r + 1 + n);
int cnt = 1;
for(int qi = 1; qi <= q; ++qi)
{
while(cnt <= n && r[cnt].first <= qry[qi].w)
{
//满足条件的情况下,利用这个城市来更新最短路
int k = r[cnt].second;
for(int i = 1; i <= n; ++i)
{
for(int j = 1; j <= n; ++j)
{
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}
}
++cnt;
}
qry[qi].ans = d[qry[qi].u][qry[qi].v];
}
sort(qry + 1, qry + 1 + q, cmpid);
cout << "Case #" << ca++ << ":" << endl;
for(int i = 1; i <= q; ++i)cout << qry[i].ans << endl; } // cout << endl << (clock() - pp) / CLOCKS_PER_SEC << endl;
return 0;
}

预处理在线

#include <bits/stdc++.h>
#define aaa cout<<233<<endl;
#define endl '\n'
#define pb push_back
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
// mt19937 rnd(time(0));
const int inf = 0x3f3f3f3f;//1061109567 > 1e9
const ll linf = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-7;
const double pi = 3.14159265358979;
const int maxn = 2e2 + 5;
const int maxm = 2e4 + 5;
const int mod = 1e9 + 7; int d[maxn][maxn][maxn];
pair<int, int> r[maxn];
int main()
{
// double pp = clock();
// freopen("233.in", "r", stdin);
// freopen("233.out", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0); int t; cin >> t;
int ca = 1;
while(t--)
{
int n, q; cin >> n >> q;
for(int i = 1; i <= n; ++i)cin >> r[i].first;
for(int i = 1; i <= n; ++i)r[i].second = i;
memset(d, inf, sizeof d);
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
cin >> d[i][j][0];
sort(r + 1, r + 1 + n);
int cnt = 1;
for(int cnt = 1; cnt <= n; ++cnt)
{
int k = r[cnt].second;
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
d[i][j][cnt] = min(d[i][j][cnt - 1], d[i][k][cnt - 1] + d[k][j][cnt - 1]);
}
// sort(r + 1, r + 1 + n, [](pair<int, int> i, pair<int, int> j){return i.second < j.second;});
cout << "Case #" << ca++ << ":" << endl;
int u, v, w; while(q--)
{
cin >> u >> v >> w;
int k = 0;
for(int i = 1; i <= n; ++i)if(r[i].first <= w)k = i;
cout << d[u][v][k] << endl;
}
} // cout << endl << (clock() - pp) / CLOCKS_PER_SEC << endl;
return 0;
}

状态不在,思路都理不清,,,emmmmmm(该收心努力了啊314,,,,,

(end)

宁夏网络赛-F-Moving On的更多相关文章

  1. 2018 宁夏省赛 F. Moving On

    题目链接 https://nanti.jisuanke.com/t/28406 大意是 有n(<=200)个城市,城市间有路(Input给了邻接矩阵)  每个城市有一个危险值,然后是q(2e4) ...

  2. ACM-ICPC 2019南昌网络赛F题 Megumi With String

    ACM-ICPC 南昌网络赛F题 Megumi With String 题目描述 给一个长度为\(l\)的字符串\(S\),和关于\(x\)的\(k\)次多项式\(G[x]\).当一个字符串\(str ...

  3. 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 F. Trig Function(切比雪夫多项式+乘法逆元)

    题目链接:哈哈哈哈哈哈 _(:з」∠)_ _(:з」∠)_ _(:з」∠)_ _(:з」∠)_ _(:з」∠)_ 哈哈哈哈哈哈,从9月16日打了这个题之后就一直在补这道题,今天终于a了,哈哈哈哈哈哈. ...

  4. 计蒜客 17119.Trig Function-切比雪夫多项式+乘法逆元 (2017 ACM-ICPC 亚洲区(西安赛区)网络赛 F)

    哈哈哈哈哈哈哈哈哈哈哈哈,终于把这道题补出来了_(:з」∠)_ 来写题解啦. _(:з」∠)_ _(:з」∠)_ _(:з」∠)_ _(:з」∠)_ _(:з」∠)_ 哈哈哈哈哈哈,从9月16日打了这 ...

  5. 沈阳网络赛 F - 上下界网络流

    "Oh, There is a bipartite graph.""Make it Fantastic." X wants to check whether a ...

  6. Hiho 1232 北京网络赛 F Couple Trees

    给两颗标号从1...n的树,保证标号小的点一定在上面.每次询问A树上的x点,和B树上的y点同时向上走,最近的相遇点和x,y到这个点的距离. 比赛的时候想用倍增LCA做,但写渣了....后来看到题解是主 ...

  7. hdu 5442 (ACM-ICPC2015长春网络赛F题)

    题意:给出一个字符串,长度是2*10^4.将它首尾相接形成环,并在环上找一个起始点顺时针或逆时针走一圈,求字典序最大的走法,如果有多个答案则找起始点最小的,若起始点也相同则选择顺时针. 分析:后缀数组 ...

  8. 2015北京网络赛 F Couple Trees 暴力倍增

    Couple Trees Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://hihocoder.com/problemset/problem/123 ...

  9. (中等) Hiho 1232 Couple Trees(15年北京网络赛F题),主席树+树链剖分。

    "Couple Trees" are two trees, a husband tree and a wife tree. They are named because they ...

  10. 2017西安网络赛 F

    f(cos(x))=cos(n∗x) holds for all xx. Given two integers nn and mm, you need to calculate the coeffic ...

随机推荐

  1. java正则表达式——规则表

    常用正则规则: 构造 匹配 字符 \\ 反斜线字符 \r 回车符 ('\u000D') \n 新行(换行)符 ('\u000A') 字符类 [abc] a.b 或 c [^abc] 任何字符,除了 a ...

  2. STATUS_STACK_BUFFER_OVERRUN不一定是栈缓冲区溢出

    STATUS_STACK_BUFFER_OVERRUN异常一般是指栈缓冲区溢出的溢出,代码为0xC0000409,消息提示一般为“Security check failure or stack buf ...

  3. 使用if和switch制作简单的年龄生肖判断

    -年 查询 --> var oDiv =document.getElementById("cont"); var oYear = document.getElementByI ...

  4. 微信小程序与云开发

    微信小程序基础概念 小程序云开发的三大基础能力:云数据库.云函数.云存储 Java.NodeJS.JavaScript.HTML5.CSS3.VueJs.ReactJs.前端工程化.前端架构 小程序开 ...

  5. flume 测试 hive sink

    测试flume,将数据送到hive表中,首先建表. create table order_flume( order_id string, user_id string, eval_set string ...

  6. (一)Cisco DHCP Snooping原理(转载)

    采用DHCP服务的常见问题架设DHCP服务器可以为客户端自动分配IP地址.掩码.默认网关.DNS服务器等网络参数,简化了网络配置,提高了管理效率.但在DHCP服务的管理上存在一些问题,常见的有: ●D ...

  7. Invalid bound statement (not found) 错误原因

    对我来说,错误的原因是因为没有配置:mybatis.mapperLocations=classpath:mybatis/mapper/*Mapper.xmlmybatis.config-locatio ...

  8. arcgis python 获得硬件id

    import time import wmi, zlib def get_cpu_info(): tmpdict = {} tmpdict["CpuCores"] = 0 c = ...

  9. Python之Lambda与三元运算

    Python之Lambda与三元运算 Lambda 运算 概念:是指一类无需定义标识符(函数名)的函数或者子程序.特点:匿名函数不使用def定义函数,使用lambda来创建匿名函数1.lambda只是 ...

  10. 子查询优化 - Hyper

    Unnesting Arbitrary Queries - T Neumann, A KemperThe Complete Story of Joins (in HyPer) - Thomas Neu ...