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;
    }
    
  • 相关阅读:
    [算法分析]计数排序
    [置顶] 基于stm32f103zet6之UC/OS_II的学习1(初步移植OS点灯大法)
    IOS开发(59)之Block Object的调用
    【译】测试员,敢问路在何方?来自微软工程师
    各种字符串hash
    hdu 2579 BFS
    qq相册
    程序人生之我们的故事:十年如歌(9)
    关联模型和无限极分类
    十大技巧破解电话面试
  • 原文地址:https://www.cnblogs.com/bzzs/p/13179014.html
Copyright © 2011-2022 走看看