• 分层图的四倍经验


    做完冻结后在做这三道题,简直爆切,直接四倍经验(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;
    }
    
  • 相关阅读:
    【存货管理】存货的计价方法
    【NHibernate】列“ReservedWord”不属于表 ReservedWords
    【MySQL】MySQL中where条件的执行分析
    brew卸载&重新安装
    mac nvm安装&使用&一些问题解决方案
    python初始环境配置
    股票数据api整理
    输入一个url到页面渲染完毕过程
    自己简单封装一个promise
    节流&防抖
  • 原文地址:https://www.cnblogs.com/bzzs/p/13179014.html
Copyright © 2020-2023  润新知