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. 学习Spring-Data-Jpa(十一)---抓取策略与实体图

    1.抓取策略 在前面说到的关联关系注解中,都有一个fetch属性,@OneToOne.@ManyToOne中都默认是FetchType.EAGER,立即获取.@OneToMany.@ManyToMan ...

  2. PowerDesigner 创建表的时候 没有自增长Id的设置项

    今天早上同事创建表的时候,在那个界面没有自增长Id的选项,当时我也纳闷,软件肯定都是一样的,设置的步骤都一样(有些配置好的 我就没改过 然后就忘了还改过些什么步骤了),结果还是没有那个选项 百度了一下 ...

  3. 控制论模型&心流模型&波模型

    1.控制论模型 这是对设定的目标,通过多次输入和输出,反馈调节,最终达成目标的方法.广泛运用于自然科学与社会科学中.反馈的周期长短决定了调节精度的大小以及达到目标的速度.反馈结果与目标背离的立即纠正, ...

  4. 虚拟变量陷阱(Dummy Variable Trap)

    虚拟变量陷阱(Dummy Variable Trap):指当原特征有m个类别时,如果将其转换成m个虚拟变量,就会导致变量间出现完全共线性的情况. 假设我们有一个特征“性别”,包含男性和女性两个类别,如 ...

  5. 【JOISC2019|2019】【20190622】cake3

    题目 \(N\) 个物品中选\(M\)个,排列成一个环:\(k_1,\cdots,k_M\)价值为: \[ \sum_{j=1}^{N}{V_i} - \sum_{j=1}^{M}|C_{k_j}- ...

  6. Hyperspectral Image Classification Using Similarity Measurements-Based Deep Recurrent Neural Networks

    用RNN来做像素分类,输入是一系列相近的像素,长度人为指定为l,相近是利用像素相似度或是范围相似度得到的,计算个欧氏距离或是SAM. 数据是两个高光谱数据 1.Pavia University,Ref ...

  7. git : error occurred during unpacking on the remote end: unpack-objects abnormal exit

    error occurred during unpacking on the remote end: unpack-objects abnormal exit git服务器磁盘满了.

  8. PHP 相关性系数计算

    相关系数公式 参考:https://baike.baidu.com/item/相关系数 PHP 实现代码 public static function calc($list) { $cv = []; ...

  9. Node add Test1

    root_group->addChild(node22); osg::Vec3f vec3f1 = node22->getBound().center(); osg::NodePathLi ...

  10. 系统重装之认识UEFI

    UEFI是一种新型的引导方式?他与传统的BIOS引导不同,传统BIOS引导需要经过(开机→BIOS初始化→BIOS自检→引导系统→进入系统)五个步骤来完成引导操作,UEFI只需要(开机→UEFI初始化 ...