• 题意:有一张有向图,每个点的权值为点\(1\)到该点的最短距离(每条边的长度为\(1\)),对于一条路径,这条路径上最多只能有一条边,这条边起点的权值不小于终点,现在要求每个点能到达路径上的点的最小权值.
  • 题解:首先我们先用bfs求出每个点的权值,并且在求的同时用桶将点存起来,方便之后枚举权值的时候用,然后我们可以将权值从大到小枚举,记\(dp_i\)是当前这个点能到达路径上的点的最小权值,对于当前的点\(u\)和它的出边\(v\),如果\(dis[u] < dis[v]\),那么我们是可以继续随便走的,所以当前状态应该是\(dp[u]=min(dp[u],dp[v])\),否则,说明我们将第二次机会用掉了,之后就只能选择第一种操作,所以我们更新的时候就不能将\(dp[v]\)(因为是从大到小枚举,所以\(dp[v]\)的状态一定是已知的)更新给当前状态,因为我们不知道\(dp[v]\)这个状态是否还用了第二次操作,所以当前状态就应该更新为\(dp[u]=min(dp[u],dis[v])\).
  • 代码:
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define pb push_back
#define me memset
#define rep(a,b,c) for(int a=b;a<=c;++a)
#define per(a,b,c) for(int a=b;a>=c;--a)
const int N = 1e6 + 10;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
using namespace std;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b) {return a/gcd(a,b)*b;} int t; int main() {
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); cin>>t;
while(t--){
int n,m;
cin>>n>>m; vector<vector<int>> v(n+1);
vector<vector<int>> tot(n+1);
vector<int> dis(n+1,INF);
vector<int> dp(n+1); int a,b; rep(i,1,m){
cin>>a>>b;
v[a].pb(b);
} queue<int> q;
q.push(1);
dis[1]=0; //bfs init
while(!q.empty()){
int cur=q.front();
q.pop();
for(auto w : v[cur]){
if(dis[cur]+1<dis[w]){
dis[w]=dis[cur]+1;
tot[dis[w]].pb(w);
q.push(w);
}
}
} rep(i,1,n){
dp[i]=dis[i];
} per(i,n-1,1){
for(auto u : tot[i]){
for(auto w : v[u]){
if(dis[w]>dis[u]) dp[u]=min(dp[u],dp[w]);
else dp[u]=min(dp[u],dis[w]);
}
}
} rep(i,1,n) cout<<dp[i]<<' ';
cout<<'\n'; } return 0;
}

Codeforces Round #693 (Div. 3) G. Moving to the Capital (图,dp)的更多相关文章

  1. Codeforces Round #582 (Div. 3)-G. Path Queries-并查集

    Codeforces Round #582 (Div. 3)-G. Path Queries-并查集 [Problem Description] 给你一棵树,求有多少条简单路径\((u,v)\),满足 ...

  2. Codeforces Round #547 (Div. 3) G 贪心

    https://codeforces.com/contest/1141/problem/G 题意 在一棵有n个点的树上给边染色,连在同一个点上的边颜色不能相同,除非舍弃掉这个点,问最少需要多少种颜色来 ...

  3. Codeforces Round #541 (Div. 2) G dp + 思维 + 单调栈 or 链表 (连锁反应)

    https://codeforces.com/contest/1131/problem/G 题意 给你一排m个的骨牌(m<=1e7),每块之间相距1,每块高h[i],推倒代价c[i],假如\(a ...

  4. Codeforces Round #481 (Div. 3) G. Petya's Exams

    http://codeforces.com/contest/978/problem/G 感冒是真的受不了...敲代码都没力气... 题目大意: 期末复习周,一共持续n天,有m场考试 每场考试有如下信息 ...

  5. Codeforces Round #346 (Div. 2) G. Fence Divercity dp

    G. Fence Divercity 题目连接: http://www.codeforces.com/contest/659/problem/G Description Long ago, Vasil ...

  6. Codeforces Round #677 (Div. 3) G. Reducing Delivery Cost(dijkstra算法)

    题目链接:https://codeforces.com/contest/1433/problem/G 题解 跑 \(n\) 遍 \(dijkstra\) 得到任意两点间的距离,然后枚举哪一条边权为 \ ...

  7. Codeforces Round #396 (Div. 2) A B C D 水 trick dp 并查集

    A. Mahmoud and Longest Uncommon Subsequence time limit per test 2 seconds memory limit per test 256 ...

  8. Codeforces Round #374 (Div. 2) A B C D 水 模拟 dp+dfs 优先队列

    A. One-dimensional Japanese Crossword time limit per test 1 second memory limit per test 256 megabyt ...

  9. Codeforces Round #386 (Div. 2)G. New Roads [构造][树]

    题目链接:G. New Roads 题意:给出n个结点,t层深度,每层有a[i]个结点,总共有k个叶子结点,构造一棵树. 分析: 考虑一颗树,如果满足每层深度上有a[i]结点,最多能有多少叶子结点 那 ...

随机推荐

  1. VsCode/Pycharm配合python env 使用

    前言 用惯了vscode,这几天试了一下pycharm,还是回来了. pycharm一个好处就是python env 环境支持的比较好, vscode虽然也支持但是要改一些东西 env的使用查看我的上 ...

  2. wpf 在不同DPI下如何在DrawingVisual中画出清晰的图形

    环境Win10 VS2017 .Net Framework4.7.1   本文仅讨论在DrawingVisual中进行的画图.   WPF单位,系统DPI,显示器DPI三者的定义及关系 WPF单位:一 ...

  3. 如果数据库上的row格式是mixed或者mixed的格式,如何对比两台数据库服务器上的数据是否一致呢

    如果数据库上的row格式是mixed或者mixed的格式,如何对比两台数据库服务器上的数据是否一致呢

  4. window安装nvm

    先说一下背景,最近做的两个项目一个是祖传angularjs1.X版本另一个是react hooks结合tailwindcss,前者angularjs的node版本比较低,而tailwindcss的no ...

  5. Table controls and tabstrip controls

    本文转载自http://www.cnblogs.com/clsoho/archive/2010/01/21/1653268.html ONTROLS Syntax Forms Declaration ...

  6. CodeMonkey少儿编程第2章 turnTo对象

    目标 了解对象的概念 了解方法与对象的关系 掌握turnTo指令的用法 在开始本章的学习之前,我们先来复习一下上一章的知识点. 在第1章中,我们学会了在这个游戏中最简单的两个指令. step x 其中 ...

  7. Linux更换软件源

    1. Ubuntu16.04 sudo cp /etc/apt/sources.list /etc/apt/sources_origin.list # 备份 sudo gedit /etc/apt/s ...

  8. Ubuntu14.04系统安装

    1. 使用U盘或光盘进行引导进入系统安装向导. 2. 安装类型选择,选择中文(简体).然后点安装ubuntu. 3. 安装ubuntu电脑必须接入外网(外网的方式有自动获取或手动编辑IP地址). 网络 ...

  9. libuv工作队列

    目录 1.说明 2.API 2.1.uv_queue_work 2.2.uv_cancel 3.代码示例 1.说明 libuv 提供了一个线程池,可用于运行用户代码,libuv 中的工作队列中的任务会 ...

  10. hook笔记①

    汇编中push 0x*** retn表示跳转到某个地址继续执行 取消hook时会在多线程环境中可能被检测 去掉函数框架可以规避寄存器cpu前后状态监测 #pragma comment(linker,& ...