2020.6.3
今天主要练习了下分层图。看洛谷题解每次都能有新收获。今天本来想练dp,后来感觉可能会太自闭了,不如先来一发最短路,毕竟看家本领不能忘。然后点进了北京某年wc的一道题,让求1-n的最短路,有k条边的费用可以减半,我一开始还是想着用spfa加一个fa数组溯回去再sort一遍来搞一搞,其实我知道这肯定不对,毕竟1-n可以有多条最短路来着。2-3-4和1-2-3-1-1这样的费用是一样,但是这样判断最短路,加上k条可以费用/2那就不一定了。还是需要更聪明一点的判断方法,结果就看到大家在刷分层图,正好想起naq17也有一张分层图的题,是有一张免费机票,就来了。
那道题我ac了,但是当时我记得我是用dijkstra + 前向星强行删边莽过去了(n不大),但是那样不太科学,毕竟如果有多于1条边可以归零,那就需要一些系统性的方法去搞了,那样我真的暴力也打不太出来了。大概是用于当我们可以改变k个图中边的权值的时候,就可以上分层图了。
研究了一下,感觉和我之前的想法很像,都是重新编号然后给层与层之间建立联系,但我当时碍于自己数学能力有限也没有想出来怎么搞。看了这个恍然大悟,艹!!np!!!然后赶紧看了看,敲了dij,然后过了这道题(感觉不像noi d1t1那样卡spfa其实spfa更快??),之后根据题单又过了几道题,连带上次naq17的,新技能get,我觉得这个有用多了hhhh,不过dp再搞一搞。
洛谷P4822 冻结
#include <bits/stdc++.h>
using namespace std;
#define limit (1000000 + 5)//防止溢出
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f
#define lowbit(i) i&(-i)//一步两步
#define EPS 1e-6
#define FASTIO ios::sync_with_stdio(false);cin.tie(0);
#define ff(a) printf("%lld\n",a );
#define pi(a,b) pair<a,b>
#define rep(i, a, b) for(int i = a; i <= b ; ++i)
#define per(i, a, b) for(int i = b ; i >= a ; --i)
#define mint(a,b,c) min(min(a,b), c)
#define MOD 998244353
#define FOPEN freopen("C:\\Users\\tiany\\CLionProjects\\acm_01\\data.txt", "rt", stdin)
typedef long long ll;
typedef unsigned long long ull;
ll read(){
ll sign = 1, x = 0;char s = getchar();
while(s > '9' || s < '0' ){if(s == '-')sign = -1;s = getchar();}
while(s >= '0' && s <= '9'){x = x * 10 + s - '0';s = getchar();}
return x * sign;
}//快读
void write(ll x){
if(x < 0) putchar('-'),x = -x;
if(x / 10) write(x / 10);
putchar(x % 10 + '0');
}
int n , m ,k;
int head[limit], cnt;
int dist[limit], vis[limit];
void init(){
cnt = 0;
memset(head, -1 , sizeof(head));
memset(dist, INF, sizeof(dist));
}
struct node{
int to, w, next;
}edge[limit<<1];
void add(int u, int v, int w = 0){
edge[cnt].to = v;
edge[cnt].w = w;
edge[cnt].next = head[u];
head[u] = cnt++;
}
struct nod{
int to, w;
bool operator<(const nod & rhs)const{
return w > rhs.w;
}
};
void dijkstra(){
priority_queue<nod>q;
q.push({1,0});
dist[1] = 0;
while(!q.empty()){
nod u = q.top();
q.pop();
vis[u.to] = 1;
for(int i = head[u.to] ; ~i ; i = edge[i].next){
int v = edge[i].to, w = edge[i].w;
if(vis[v])continue;
if(dist[u.to] + w < dist[v]){
dist[v] = dist[u.to] + w;
q.push({v, dist[v]});
}
}
}
}
int main() {
#ifdef LOCAL
FOPEN;
#endif
n = read() ,m = read(), k = read();
init();
rep(j ,1, m){
int x = read(), y = read(), w = read();
rep(i ,0, k){
add(i * n + x, i * n + y, w);
add(i * n + y , i * n + x , w);
}
rep(i ,0,k-1 ){
add(i * n + x, (i + 1) * n + y, w / 2);
add(i * n + y , (i + 1) * n + x , w/ 2);
}
}
dijkstra();
int ans = INF;
rep(i ,0 , k){
ans = min(ans, dist[i * n + n]);
}
write(ans);
return 0;
}
JLOI P4568
#include <bits/stdc++.h>
using namespace std;
#define limit (10000 + 5)//防止溢出
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f
#define lowbit(i) i&(-i)//一步两步
#define EPS 1e-6
#define FASTIO ios::sync_with_stdio(false);cin.tie(0);
#define ff(a) printf("%lld\n",a );
#define pi(a,b) pair<a,b>
#define rep(i, a, b) for(int i = a; i <= b ; ++i)
#define per(i, a, b) for(int i = b ; i >= a ; --i)
#define mint(a,b,c) min(min(a,b), c)
#define MOD 998244353
#define FOPEN freopen("C:\\Users\\tiany\\CLionProjects\\acm_01\\data.txt", "rt", stdin)
typedef long long ll;
typedef unsigned long long ull;
ll read(){
ll sign = 1, x = 0;char s = getchar();
while(s > '9' || s < '0' ){if(s == '-')sign = -1;s = getchar();}
while(s >= '0' && s <= '9'){x = x * 10 + s - '0';s = getchar();}
return x * sign;
}//快读
void write(ll x){
if(x < 0) putchar('-'),x = -x;
if(x / 10) write(x / 10);
putchar(x % 10 + '0');
}
int n , m ,k;
int head[limit], cnt;
int dist[limit], vis[limit];
void init(){
cnt = 0;
memset(head, -1 , sizeof(head));
memset(dist, INF, sizeof(dist));
}
struct node{
int to, w, next;
}edge[limit<<1];
void add(int u, int v, int w = 0){
edge[cnt].to = v;
edge[cnt].w = w;
edge[cnt].next = head[u];
head[u] = cnt++;
}
struct nod{
int to, w;
bool operator<(const nod & rhs)const{
return w > rhs.w;
}
};
void dijkstra(int s){
priority_queue<nod>q;
q.push({s,0});
dist[s] = 0;
while(!q.empty()){
nod u = q.top();
q.pop();
vis[u.to] = 1;
for(int i = head[u.to] ; ~i ; i = edge[i].next){
int v = edge[i].to, w = edge[i].w;
if(vis[v])continue;
if(dist[u.to] + w < dist[v]){
dist[v] = dist[u.to] + w;
q.push({v, dist[v]});
}
}
}
}
int main() {
#ifdef LOCAL
FOPEN;
#endif
n = read() ,m = read(), k = read();
int s= read() ,t = read();
init();
rep(q, 1, m){
int x= read(), y = read() , w = read();
rep(i ,0 , k){
add(i * n + x , i * n + y , w);
add(i * n + y , i * n + x , w);
}
rep(i , 0 , k - 1){
add(i * n + x , (i + 1) * n + y , 0);
add(i * n + y , (i + 1) * n + x, 0);
}
}
dijkstra(s);
int ans = INF;
rep(i ,0, k)ans = min(ans, dist[i * n + t]);
write(ans);
return 0;
}
洛谷P2939
Revamping Trails
#include <bits/stdc++.h>
using namespace std;
#define limit (5000000 + 5)//防止溢出
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f
#define lowbit(i) i&(-i)//一步两步
#define EPS 1e-6
#define FASTIO ios::sync_with_stdio(false);cin.tie(0);
#define ff(a) printf("%lld\n",a );
#define pi(a,b) pair<a,b>
#define rep(i, a, b) for(int i = a; i <= b ; ++i)
#define per(i, a, b) for(int i = b ; i >= a ; --i)
#define mint(a,b,c) min(min(a,b), c)
#define MOD 998244353
#define FOPEN freopen("C:\\Users\\tiany\\CLionProjects\\acm_01\\data.txt", "rt", stdin)
typedef long long ll;
typedef unsigned long long ull;
ll read(){
ll sign = 1, x = 0;char s = getchar();
while(s > '9' || s < '0' ){if(s == '-')sign = -1;s = getchar();}
while(s >= '0' && s <= '9'){x = x * 10 + s - '0';s = getchar();}
return x * sign;
}//快读
void write(ll x){
if(x < 0) putchar('-'),x = -x;
if(x / 10) write(x / 10);
putchar(x % 10 + '0');
}
int n , m ,k;
int head[limit], cnt;
int dist[limit], vis[limit];
void init(){
cnt = 0;
memset(head, -1 , sizeof(head));
memset(dist, INF, sizeof(dist));
}
struct node{
int to, w, next;
}edge[limit<<1];
void add(int u, int v, int w = 0){
edge[cnt].to = v;
edge[cnt].w = w;
edge[cnt].next = head[u];
head[u] = cnt++;
}
struct nod{
int to, w;
bool operator<(const nod & rhs)const{
return w > rhs.w;
}
};
void dijkstra(int s=1){
priority_queue<nod>q;
q.push({s,0});
dist[s] = 0;
while(!q.empty()){
nod u = q.top();
q.pop();
vis[u.to] = 1;
for(int i = head[u.to] ; ~i ; i = edge[i].next){
int v = edge[i].to, w = edge[i].w;
if(vis[v])continue;
if(dist[u.to] + w < dist[v]){
dist[v] = dist[u.to] + w;
q.push({v, dist[v]});
}
}
}
}
int main() {
#ifdef LOCAL
FOPEN;
#endif
n = read() ,m = read(), k = read();
init();
rep(q, 1, m){
int x= read(), y = read() , w = read();
rep(i ,0 , k){
add(i * n + x , i * n + y , w);
add(i * n + y , i * n + x , w);
}
rep(i , 0 , k - 1){
add(i * n + x , (i + 1) * n + y , 0);
add(i * n + y , (i + 1) * n + x, 0);
}
}
dijkstra();
int ans = INF;
rep(i ,0, k)ans = min(ans, dist[i * n + n]);
write(ans);
return 0;
}