zoukankan      html  css  js  c++  java
  • poj3255 Roadblocks

    /*
      解析可看:
      http://blog.csdn.net/arthurfree/article/details/37884463
      
      这次debug时,发现犯了个隐蔽的小错误,让我找了好久才发现...
      for (int i = 0; i < G[v].size(); i++) 
      我一开始,把这句里的v,手误打成了i,然后检查时还一直没能发现T^T
    */



    #include <iostream>
    #include <queue>
    #include <vector>
    using namespace std;
    int N, R;
    int f, t, c;
    const int MAX_N = 1e5 + 10;
    const int INF = 1e6;
    struct edge
    {
    	int to, cost;
    	edge (int t, int c)
    	{
    		to = t; cost = c;
    	}
    };
    typedef pair <int, int> P;
    vector<edge> G[MAX_N]; //图的邻接表表示
    
    int dist[MAX_N]; // 最短距离
    int dist2[MAX_N]; // 次短距离
    
    void solve()
    {
    	priority_queue<P, vector<P>, greater<P> > que;
    	fill (dist, dist + N, INF);
    	fill (dist2, dist2 + N, INF);
    	dist[0] = 0;
    	que.push( P(0,0) );
    	
    	while (!que.empty())
    	{
    		P p = que.top(); que.pop();
    		int v = p.second, d = p.first; // v为标号,d为出队的边的cost
    		if ( dist2[v] < d ) continue;
    		for (int i = 0; i < G[v].size(); i++) 
    		{
    			edge&e = G[v][i];
    			int d2 = d + e.cost;
    			if (dist[e.to] > d2) 
    			{
    				swap(dist[e.to], d2); //巧妙,一箭双雕,将判断是否更新次小值时,还要再次用到的d,一并更新,否则要写三变量交换了
    				que.push( P(dist[e.to], e.to) );
    			}
    			if (dist2[e.to] > d2 && d2 > dist[e.to])
    			{
    				dist2[e.to] = d2;
    				que.push( P(dist2[e.to], e.to) );
    			}
     		}	
    	}
    	cout << dist2[N - 1] << endl;
    }
    int main()
    {
    	cin >> N >> R;
    	for (int i = 0; i < N; i++)
    	G[i].clear();
    	
    	for (int i = 0; i < R; i++)
    	{
    //		edge now; int from;
    //		cin >> from >> now.to >> now.cost;
    //		G[from].push_back(now);
    		
    		cin >> f >> t >> c;
    		f--; t--;
    		G[f].push_back(edge(t, c));
    		G[t].push_back(edge(f, c));
    	}
    	solve();
    	return 0;
    }


  • 相关阅读:
    解决打开GitHub慢的问题
    RestFramework规范简介
    在Linux中持久化运行项目
    Linux安装Mysql
    Java基础内容汇总[持续更新]
    Elasticsearch内容汇总[持续更新]
    深入源码理解SpringBean生命周期
    利用JVM钩子函数优雅关闭线程池
    聊聊消息队列高性能的秘密——零拷贝技术
    Elasticsearch性能优化汇总——写入&搜索
  • 原文地址:https://www.cnblogs.com/mofushaohua/p/7789493.html
Copyright © 2011-2022 走看看