zoukankan      html  css  js  c++  java
  • PKU 1511 Invitation Cards (SPFA+邻接表)

    题目链接:点击打开链接

            题目需要求从原点到所有点的最短距离之和和所有点到原点的最短距离之和,在求所有点到原点最短距离的时候用到了一个技巧:即把图反向,求原点到所有其他点的最短距离,这样用一次SPFA就可以将所有点到原点的最短距离求出来了。

            另外也没什么好说的,纯SPFA。另外用优化到VlogE的dijkstra貌似也能过,有空的时候再写个。

    代码如下:

    #include <iostream>
    #include <algorithm>
    #include <string>
    #include <sstream>
    #include <queue>
    using namespace std;
    
    #define MAX 1000009
    #define INF 1<<30
    struct ENode 
    {
    	int to, cost, next;
    } enode[MAX * 4];
    
    int NE = 0;
    int OriginHead[MAX], RevHead[MAX];
    int dist[MAX];
    
    void insertEdge( int from, int to, int cost )
    {
    	enode[NE].cost = cost; enode[NE].to = to; enode[NE].next = OriginHead[from]; OriginHead[from] = NE++;
    	enode[NE].cost = cost; enode[NE].to = from; enode[NE].next = RevHead[to]; RevHead[to] = NE++;
    }
    
    void initDist( int n )
    {
    	for (int i=0; i<=n; i++) dist[i] = INF;
    	dist[1] = 0;
    }
    
    int inQueue[MAX];
    queue<int> Q;
    void SPFA( int *Head )
    {
    	inQueue[1] = 1;
    	Q.push(1);
    	while ( ! Q.empty() )
    	{
    		int q = Q.front();
    		Q.pop();
    		inQueue[q] = 0;
    		for ( int i=Head[q]; i!=-1; i=enode[i].next )
    		{
    			int e = enode[i].to;
    			if ( dist[q] + enode[i].cost < dist[e] ) 
    			{
    				dist[e] = dist[q] + enode[i].cost;
    				if ( !inQueue[e] )
    				{
    					inQueue[e] = 1;
    					Q.push( e );
    				}
    			}
    		}
    	}
    }
    
    long long getTotal( int n )
    {
    	long long ret = 0;
    	for (int i=2; i<=n; i++)
    		ret += dist[i];
    	return ret;
    }
    
    int main( )
    {
    	//cout << (int)(1<<30) << endl;
    	int T;
    	cin >> T;
    	
    	while( T-- )
    	{
    		int n, m;
    		scanf("%d%d", &n, &m);
    
    		memset( OriginHead, -1, sizeof(OriginHead) );
    		memset( RevHead, -1, sizeof(RevHead) );
    		NE = 0;
    		for (int i=0; i<m; i++)
    		{
    			int from, to, cost;
    			scanf( "%d%d%d", &from, &to, &cost );
    			insertEdge( from, to, cost );
    		}
    
    		initDist( n );
    		memset( inQueue, 0, sizeof(inQueue) );
    		SPFA( OriginHead );
    		long long int ans = 0;
    		ans += getTotal( n );
    
    		//cout << ans << endl;
    		initDist( n );
    		memset( inQueue, 0, sizeof(inQueue) );
    		SPFA( RevHead );
    		ans += getTotal( n );
    
    		cout << ans << endl;
    	}
    
    	return 0;
    }


     

  • 相关阅读:
    Android2.3.7源码结构分析
    【开源推荐】AllJoyn:打造全球物联网的通用开源框架
    Ubuntu12.04安装JDK6
    ubuntu12.04升级后找不到共享目录
    Windows Embedded Compact 7新特性
    Windows Embedded Compact 2013 安装体验
    Windows CE,你妈吗喊你在多核上玩玩
    April Fools Contest 2017 C
    April Fools Contest 2017 B
    April Fools Contest 2017 A
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3228617.html
Copyright © 2011-2022 走看看