zoukankan      html  css  js  c++  java
  • 【图论】POJ-3255 次短路径

    一、题目

    Description

    Bessie has moved to a small farm and sometimes enjoys returning to visit one of her best friends. She does not want to get to her old home too quickly, because she likes the scenery along the way. She has decided to take the second-shortest rather than the shortest path. She knows there must be some second-shortest path.

    The countryside consists of R (1 ≤ R ≤ 100,000) bidirectional roads, each linking two of the N (1 ≤ N ≤ 5000) intersections, conveniently numbered 1..N. Bessie starts at intersection 1, and her friend (the destination) is at intersection N.

    The second-shortest path may share roads with any of the shortest paths, and it may backtrack i.e., use the same road or intersection more than once. The second-shortest path is the shortest path whose length is longer than the shortest path(s) (i.e., if two or more shortest paths exist, the second-shortest path is the one whose length is longer than those but no longer than any other path).

    Input

    Line 1: Two space-separated integers: N and R

    Line 1: Two space-separated integers: N and R 
    Lines 2..R+1: Each line contains three space-separated integers: AB, and D that describe a road that connects intersections A and B and has length D (1 ≤ D ≤ 5000)

    Output

    Line 1: The length of the second shortest path between node 1 and node N

    Sample Input

    4 4
    1 2 100
    2 4 200
    2 3 250
    3 4 100
    

    Sample Output

    450
    

    Hint

    Two routes: 1 -> 2 -> 4 (length 100+200=300) and 1 -> 2 -> 3 -> 4 (length 100+250+100=450)

    二、思路&心得

    • 定义权值的最大值时,尽可能大,综合题目的条件,比如这题的MAX_D就应该大于两倍的D最大值
    • 注意题目条件:比如这题存在自环边、双向边,同一条道路可以经过
    • 图论中,注意根据题目图的特点选择对应的算法,比如这题为稀疏图,应选择邻接表进行表示图

    三、代码

    #include<cstdio>
    #include<vector>
    #include<algorithm>
    #define MAX_SIZE 5005
    #define MAX_D 10005
    using namespace std;
    
    struct edge {
    	int to, cost;
    };
    
    vector<edge> G[MAX_SIZE];
    
    int N, R;
    int dist[MAX_SIZE];
    int dist2[MAX_SIZE];
    int visit[MAX_SIZE];
    int visit2[MAX_SIZE];
    
    void Dijkstra(int s) {
    	dist[s] = 0;
    	while (true) {
    		int min_D = MAX_D;
    		int index;
    		int d, flag = 0;
    		for (int j = 1; j <= N; j ++) {
    			if (!visit[j] && dist[j] < min_D) {
    				min_D = dist[j];
    				index = j;
    				flag = 1;
    			} else if (!visit2[j] && dist2[j] < min_D) {
    				min_D = dist[j];
    				index = j;
    				flag = 2;
    			}
    		}
    		
    		if (!flag) break;
    		else if (flag == 1) {
    			visit[index] = 1;
    			d = dist[index];
    		}
    		else if (flag == 2) {
    			visit2[index] = 1;
    			d = dist2[index];
    		}
    		
    		for (int k = 0; k < G[index].size(); k ++) {
    			edge e = G[index][k];
    			int temp = e.cost + d;
    			if (!visit[e.to] && temp < dist[e.to]) {
    				swap(dist[e.to], temp);
    			}
    			if (dist[e.to] < temp && temp < dist2[e.to]) {
    				dist2[e.to] = temp;
    				visit2[e.to] = 0;
    			}
    		}
    	}
    	printf("%d
    ", dist2[N]);
    }
    
    void solve() {
    	for (int i = 1; i <= N; i ++) {
    		visit[i] = 0;
    		visit2[i] = 1;	
    		dist[i] = MAX_D;
    		dist2[i] = MAX_D;
    		G[i].clear();
    	}
    	int from;
    	edge e;
    	for (int i = 1; i <= R; i ++) {
    		scanf("%d %d %d", &from, &e.to, &e.cost);
    		G[from].push_back(e);
    		swap(from, e.to);
    		G[from].push_back(e);
    	}
    	Dijkstra(1);	
    }
    
    int main() {
    	while (~scanf("%d %d", &N, &R)) { 
    		solve();	
    	}
    	return 0; 
    }
    
  • 相关阅读:
    Windows 2008 R2 远程桌面服务(八)远程桌面服务器安全设置
    从硬盘上安装SQLServer2005的问题
    在 Windows server 2008 下计划任务无法正常执行bat批处理文件
    两部搞定windows server 2008 R2 中IE8的增强安全配置功能
    Microsoft SQL Server 2005 Service Pack 4 RTM
    学习地址
    Windows 2008 远程桌面如何设置两个用户共享一个会话
    Windows Server 2008 启用无线网卡
    远程桌面连接指定会话(Session)
    固定宽度弹性布局(以适应各种各辨率)
  • 原文地址:https://www.cnblogs.com/CSLaker/p/7281001.html
Copyright © 2011-2022 走看看