当前位置:网站首页>Travel (split points and layers)
Travel (split points and layers)
2022-07-26 01:29:00 【Dimple】
https://ac.nowcoder.com/acm/contest/37782/I
The question
Given n A little bit m An undirected graph of edges , Each side has a cost w i w_i wi.
Starting from 1, The finish for n.
For a path from the beginning to the end , It takes you x x x.
ask , What is the minimum cost from the beginning to the end ?
( 1 ≤ n , x ≤ 1 0 5 , n − 1 ≤ m ≤ 2 × 1 0 5 ) (1≤n,x≤10^5,\ n−1≤m≤2×10^5) (1≤n,x≤105, n−1≤m≤2×105)
Ideas
Analyze the topic and know , For a path , Its cost depends not only on the sum of edge weights , It also depends on the path length . The longer the path , More even points , It's going to cost a lot x. So you can't simply run the shortest way .
The cost from one point to the next is different , In addition to the edge right , Also consider whether the current point is odd or even . Odd and even points are connected .
You can split each point into two points , They are odd points and even points .
Put all the odd points on the first layer , All even points are placed on the second floor . Connect the edges from the first floor to the second floor every time , From the second floor to the first floor , In this way, odd points and even points are connected .
Because it costs to reach even points x, This cost can be added to the edge right , When connecting the edge from the first floor to the second floor , Edge weight plus x.
Then from the first floor 1 Start running at No , Take it to the first floor n Point number and the second floor n The minimum value of the shortest distance of point is the minimum cost .
Code
#include<bits/stdc++.h>
using namespace std;
#define Ios ios::sync_with_stdio(false),cin.tie(0)
#define int long long
const int N = 200010, mod = 1e9+7;
int T, n, m;
int a[N];
int k;
int dist[N], f[N];
vector<PII> e[N];
int ans;
void dij()
{
priority_queue<PII, vector<PII>, greater<PII> > que;
que.push({
0, 1});
for(int i=1;i<=2*n;i++) dist[i] = 1e18;
dist[1] = 0;
while(que.size())
{
int x = que.top().se; que.pop();
if(f[x]) continue;
f[x] = 1;
if(x == n || x == n + n){
ans = dist[x];
return;
}
for(auto it : e[x])
{
int tx = it.fi, dis = it.se;
if(dist[tx] > dist[x] + dis){
dist[tx] = dist[x] + dis;
que.push({
dist[tx], tx});
}
}
}
}
signed main(){
Ios;
cin >> n >> m >> k;
while(m--)
{
int x, y, z; cin >> x >> y >> z;
int nex = n + x, ney = n + y;
e[x].push_back({
ney, z + k});
e[ney].push_back({
x, z});
e[nex].push_back({
y, z});
e[y].push_back({
nex, z + k});
}
dij();
cout << ans;
return 0;
}
Experience
The idea of dismantling points should be mastered .
seek 1-n All prime factors of each number in
The complexity of directly decomposing prime factors for each number is O ( n n ) O(n \sqrt n) O(nn),1e6 You can't run .
Think the other way around , Consider which numbers each prime factor is in .
For each prime factor , It must be the prime factor of all its multiples .
Then it can be in the sieve , Traverse all its multiples , Put the current prime factor into the multiple vector in , At the same time, mark the number as a non prime number , Will not traverse its multiples .
such , The Egyptian sieve ensures that the traversal is prime , Traversing all multiples ensures that it is a factor , stay O ( n l o g n ) O(nlogn) O(nlogn) Within the complexity of 1-n this n All prime factors of numbers .
#include<bits/stdc++.h>
using namespace std;
const int N = 2000010, mod = 1e9+7;
int T, n, m;
vector<int> v[N];
int f[N];
void Prim()
{
for(int i=2;i<=n;i++)
{
if(f[i]) continue;
v[i].push_back(i);
for(int j=i+i;j<=n;j+=i){
v[j].push_back(i);
f[j] = 1;
}
}
}
signed main(){
cin >> n;
Prim();
for(int i=1;i<=n;i++)
{
cout << i << " : ";
for(int x : v[i]) cout << x << " ";
cout << endl;
}
return 0;
}
Magic number
The question
Find all positive integers x , bring a ≡ b ≡ c ( m o d x ) a\equiv b \equiv c\ (\bmod x) a≡b≡c (modx) .
Please output all... From small to large x Possible values , If there are infinite possibilities x, The output -1 .
Ideas
Find the two numbers first x, Then judge that another number symbol does not match .
a ≡ b ( m o d x ) a\equiv b\ (\bmod x) a≡b (modx) Turn into ( a − b ) m o d x = 0 (a-b)\bmod x = 0 (a−b)modx=0.
that x yes ∣ a − b ∣ |a-b| ∣a−b∣ The divisor of .
Traverse all its divisors to see c Whether it conforms to .
When a=b=c when ,x Take any value .
otherwise Take two different numbers Subtract for divisor .
Code
#include<bits/stdc++.h>
using namespace std;
const int N = 200010, mod = 1e9+7;
int T, n, m;
int a[N];
signed main(){
cin >> T;
while(T--)
{
int x, y, z;
cin >> x >> y >> z;
if(x == y && y == z){
cout << -1 << endl;
continue;
}
set<int> st;
int n;
if(x != y) n = abs(x - y);
else n = abs(x - z);
for(int i=1;i<=n/i;i++)
{
if(n % i == 0)
{
if(x % i == y % i && x % i == z % i){
st.insert(i);
}
int tt = n/i;
if(tt == i) continue;
if(x % tt == y % tt && x % tt == z % tt){
st.insert(tt);
}
}
}
for(int x : st) cout << x << " ";
cout << endl;
}
return 0;
}
x mod y = 0, y It must be x The divisor of .
边栏推荐
- 数据库系统原理与应用教程(053)—— MySQL 查询(十五):字符型函数的用法
- I want to know how much the Commission is for opening an account. Is it safe to open an account on your mobile phone
- “元气可乐”不是终点,“中国可乐”才是
- [CTF] crypto preliminary basic outline
- 网络性能评估工具 ping/mtr
- PyCharm在创建py文件时自动添加头部注释
- NiO simple example
- poj1521
- Tutorial on the principle and application of database system (057) -- MySQL exercises
- 机器学习:贝叶斯网络
猜你喜欢

FreeBSD bnxt以太网驱动源码阅读记录二:

C语言中的整型数据类型(你真的了解吗)

Machine learning: Bayesian Networks

Prime Ring Problem

《分布式微服务电商》专题(一)-项目简介

Practice sharing of monorepo based on yarn1.x

Handler消息机制-FWK层

The second China rust developer conference is coming, and the complete agenda has been exposed!

2022年最新北京建筑八大员(材料员)模拟考试试题及答案

Codeforces Round #810 (Div. 2)A~C
随机推荐
Dijkstra 求最短路
MDK编译过程及ARM编译工具链
3059. Sculpture (jzoj)
Network performance evaluation tool ping/mtr
PTGui Pro12垂直线纠正
zeromq浅析
Basic version of Google browser debugging tool (I)
Introduction to API testing
Cross linguistic transfer of correlations between parts of speech and Gazette Features Reading Notes
数据库系统原理与应用教程(054)—— MySQL 查询(十六):日期时间型函数的用法
B - Krypton Factor (dfs+ backtracking method)
Prime Ring Problem
Failed to load DLL
API测试简介
1.30 upgrade bin file, add suffix and file length
[Unity] 二维洞穴地图随机生成
NIO简易示例
Detailed explanation of redis data structure, combined with books
链表相关面试题
NodeJS 基于 Dapr 构建云原生微服务应用,从 0 到 1 快速上手指南