zoukankan      html  css  js  c++  java
  • 分层图的四倍经验

    做完冻结后在做这三道题,简直爆切,直接四倍经验(STO)

    飞行路线

    几乎跟冻结一模一样就不讲啦

    #include <bits/stdc++.h>
    using namespace std;
    int n , m , k , ans = 0x3ffffff , s , ee;
    int dis[10000010] , vis[10000010];
    vector<pair<int , int> > e[1000010];
    void work(){
    	priority_queue<pair<int , int> > q; 
    	memset(dis , 127 , sizeof(dis));
    	dis[s + k * n] = 0;
    	q.push(make_pair(0 , s + k * n));
    	while(!q.empty()){
    		int x = q.top().second;
    		q.pop();
    		if(vis[x]) continue;
    		vis[x] = 1;
    		for(int i = 0; i < e[x].size(); i++){
    			int nx = e[x][i].first , w = e[x][i].second;
    			if(dis[nx] > dis[x] + w){
    				dis[nx] = dis[x] + w;
    				q.push(make_pair(-dis[nx] , nx));
    			}
    		}
    	}
    }
    int main(){
    	cin >> n >> m >> k >> s >> ee;
    	s++ , ee++;
    	for(int i = 1; i <= m; i++){
    		int x , y , z;
    		cin >> x >> y >> z;
    		x++ , y++;
    		e[x].push_back(make_pair(y , z));
    		e[y].push_back(make_pair(x , z));
    		for(int j = 1; j <= k; j++){
    			e[x + j * n].push_back(make_pair(y + n * (j - 1) , 0));
    			e[x + j * n].push_back(make_pair(y + n * j , z));
    			e[y + j * n].push_back(make_pair(x + n * (j - 1) , 0));
    			e[y + j * n].push_back(make_pair(x + n * j , z));
    		}
    	}
    	work();
    	for(int i = 0; i <= k; i++) ans = min(dis[ee + i * n] , ans);
    	cout << ans;
    	return 0;
    }
    

    Telephone Lines S

    这道题改了一点点,就是让你输出的是最长的一条电话线,在跑最短路的时候改一下就OK了:

    #include <bits/stdc++.h>
    using namespace std;
    int n , m , k , ans = 0x3ffffff , s , ee;
    int dis[10000010] , vis[10000010];
    vector<pair<int , int> > e[1000010];
    void work(){
    	priority_queue<pair<int , int> > q; 
    	memset(dis , 127 , sizeof(dis));
    	dis[s + k * n] = 0;
    	q.push(make_pair(0 , s + k * n));
    	while(!q.empty()){
    		int x = q.top().second;
    		q.pop();
    		if(vis[x]) continue;
    		vis[x] = 1;
    		for(int i = 0; i < e[x].size(); i++){
    			int nx = e[x][i].first , w = e[x][i].second;
    			if(dis[nx] > dis[x] + w){
    				dis[nx] = dis[x] + w;
    				q.push(make_pair(-dis[nx] , nx));
    			}
    		}
    	}
    }
    int main(){
    	cin >> n >> m >> k >> s >> ee;
    	s++ , ee++;
    	for(int i = 1; i <= m; i++){
    		int x , y , z;
    		cin >> x >> y >> z;
    		x++ , y++;
    		e[x].push_back(make_pair(y , z));
    		e[y].push_back(make_pair(x , z));
    		for(int j = 1; j <= k; j++){
    			e[x + j * n].push_back(make_pair(y + n * (j - 1) , 0));
    			e[x + j * n].push_back(make_pair(y + n * j , z));
    			e[y + j * n].push_back(make_pair(x + n * (j - 1) , 0));
    			e[y + j * n].push_back(make_pair(x + n * j , z));
    		}
    	}
    	work();
    	for(int i = 0; i <= k; i++) ans = min(dis[ee + i * n] , ans);
    	cout << ans;
    	return 0;
    }
    

    Revamping Trails G

    也是很简单的,我就不水了:

    #include <bits/stdc++.h>
    using namespace std;
    int n , m , k , ans = 0x3ffffff;
    int dis[1000010] , vis[1000010];
    vector<pair<int , int> > e[1000010];
    void work1(){
    	priority_queue<pair<int , int> > q; 
    	memset(dis , 127 , sizeof(dis));
    	dis[1 + k * n] = 0;
    	q.push(make_pair(0 , 1 + k * n));
    	while(!q.empty()){
    		int x = q.top().second;
    		q.pop();
    		if(vis[x]) continue;
    		vis[x] = 1;
    		for(int i = 0; i < e[x].size(); i++){
    			int nx = e[x][i].first , w = e[x][i].second;
    			if(dis[nx] > dis[x] + w){
    				dis[nx] = dis[x] + w;
    				q.push(make_pair(-dis[nx] , nx));
    			}
    		}
    	}
    }
    int main(){
    	cin >> n >> m >> k;
    	for(int i = 1; i <= m; i++){
    		int x , y , z;
    		cin >> x >> y >> z;
    		e[x].push_back(make_pair(y , z));
    		e[y].push_back(make_pair(x , z));
    		for(int j = 1; j <= k; j++){
    			e[x + j * n].push_back(make_pair(y + n * (j - 1) , 0));
    			e[x + j * n].push_back(make_pair(y + n * j , z));
    			e[y + j * n].push_back(make_pair(x + n * (j - 1) , 0));
    			e[y + j * n].push_back(make_pair(x + n * j , z));
    		}
    	}
    	work1();
    	for(int i = 0; i <= k; i++) ans = min(dis[n + i * n] , ans);
    	cout << ans;
    	return 0;
    }
    
  • 相关阅读:
    机器学习入门实践——线性回归&非线性回归&mnist手写体识别
    基于OpenCV的摄像头采集印刷体数字识别
    使用rviz实现本地计算机绘制机器人路径
    从0开始的FreeRTOS(4)
    从0开始的FreeRTOS(3)
    从0开始的FreeRTOS(2)
    从0开始的FreeRTOS(1)
    Robomaster电控入门(8)RTOS
    Robomaster电控入门(7)双轴云台控制
    计算机与网络课程设计开发纪要
  • 原文地址:https://www.cnblogs.com/bzzs/p/13179014.html
Copyright © 2011-2022 走看看