宁夏网络赛-F-Moving On
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的更多相关文章
- 2018 宁夏省赛 F. Moving On
题目链接 https://nanti.jisuanke.com/t/28406 大意是 有n(<=200)个城市,城市间有路(Input给了邻接矩阵) 每个城市有一个危险值,然后是q(2e4) ...
- ACM-ICPC 2019南昌网络赛F题 Megumi With String
ACM-ICPC 南昌网络赛F题 Megumi With String 题目描述 给一个长度为\(l\)的字符串\(S\),和关于\(x\)的\(k\)次多项式\(G[x]\).当一个字符串\(str ...
- 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 F. Trig Function(切比雪夫多项式+乘法逆元)
题目链接:哈哈哈哈哈哈 _(:з」∠)_ _(:з」∠)_ _(:з」∠)_ _(:з」∠)_ _(:з」∠)_ 哈哈哈哈哈哈,从9月16日打了这个题之后就一直在补这道题,今天终于a了,哈哈哈哈哈哈. ...
- 计蒜客 17119.Trig Function-切比雪夫多项式+乘法逆元 (2017 ACM-ICPC 亚洲区(西安赛区)网络赛 F)
哈哈哈哈哈哈哈哈哈哈哈哈,终于把这道题补出来了_(:з」∠)_ 来写题解啦. _(:з」∠)_ _(:з」∠)_ _(:з」∠)_ _(:з」∠)_ _(:з」∠)_ 哈哈哈哈哈哈,从9月16日打了这 ...
- 沈阳网络赛 F - 上下界网络流
"Oh, There is a bipartite graph.""Make it Fantastic." X wants to check whether a ...
- Hiho 1232 北京网络赛 F Couple Trees
给两颗标号从1...n的树,保证标号小的点一定在上面.每次询问A树上的x点,和B树上的y点同时向上走,最近的相遇点和x,y到这个点的距离. 比赛的时候想用倍增LCA做,但写渣了....后来看到题解是主 ...
- hdu 5442 (ACM-ICPC2015长春网络赛F题)
题意:给出一个字符串,长度是2*10^4.将它首尾相接形成环,并在环上找一个起始点顺时针或逆时针走一圈,求字典序最大的走法,如果有多个答案则找起始点最小的,若起始点也相同则选择顺时针. 分析:后缀数组 ...
- 2015北京网络赛 F Couple Trees 暴力倍增
Couple Trees Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://hihocoder.com/problemset/problem/123 ...
- (中等) Hiho 1232 Couple Trees(15年北京网络赛F题),主席树+树链剖分。
"Couple Trees" are two trees, a husband tree and a wife tree. They are named because they ...
- 2017西安网络赛 F
f(cos(x))=cos(n∗x) holds for all xx. Given two integers nn and mm, you need to calculate the coeffic ...
随机推荐
- 【NOIP2014】真题回顾
题目链接 生活大爆炸版石头剪刀布 就是个模拟,不说了 联合权值 枚举每个点,统计它任意两个儿子的联合权值,统计的时候维护sum和max就行了 飞扬的小鸟 比较好的DP题,不难想到用dp[i][j]表示 ...
- shell脚本编程基础之case语句
基础简介 脚本编程分为: 面向过程 选择结构:if语句,单分支.双分支.多分支:case语句 控制结构:顺序结构(默认) 循环结构:for.while.until 面向对象 case语句结构 case ...
- Pytest权威教程(官方教程翻译)
Pytest权威教程01-安装及入门 Pytest权威教程02-Pytest 使用及调用方法 Pytest权威教程03-原有TestSuite的执行方法 Pytest权威教程04-断言的编写和报告 P ...
- 升级项目版本:SpringBoot1.5.x到SpringBoot2.0.x
1.升级版本的选择 首先去spring的官网看一下最新的版本与版本之间的依赖
- h5的复制功能的使用,Clipboard.js的使用,主要是在app里面使用
app中使用,框架用的是vue.js 1.显示要下载这个Clipboard.js插件 package-lock.json里面 "clipboard-polyfill": { &qu ...
- D3.js的v5版本入门教程(第五章)—— 选择、插入、删除元素
D3.js的v5版本入门教程(第五章) 1.选择元素 现在我们已经知道,d3.js中选择元素的函数有select()和selectAll(),下面来详细讲解一下 假设我们的<body>中有 ...
- jsp页面获取后台传过来的list集合的长度
在jsp页面导入函数标签库: <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"% ...
- Mac 上ssh远程连接Linux服务器提示Host key verification failed.
当我们对重装远程服务器的时候会出现Host key verification failed问题 解决办法: rm -rf ~/.ssh/known_hosts 重新ssh连接,OK!
- oracle plsql 异常
set serveroutput on DECLARE pename emp.ename%type; begin '; exception when no_data_found then dbms ...
- 【tensorflow基础】Tensorpack-API
安装 pip install tensorpack 使用 参考 1. Tensorpack: 2. Tensorpack,一个基于TensorFlow的神经网络训练界面,源码包含很多示例: 完