POJ 1511 Invitation Cards(最短路spfa演算法)

KeepTing發表於2017-03-05
Invitation Cards
Time Limit: 8000MS      Memory Limit: 262144K
Total Submissions: 21615       Accepted: 7089

Description
In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia are aware of this fact. They want to propagate theater and, most of all, Antique Comedies. They have printed invitation cards with all the necessary information and with the programme. A lot of students were hired to distribute these invitations among the people. Each student volunteer has assigned exactly one bus stop and he or she stays there the whole day and gives invitation to people travelling by bus. A special course was taken where students learned how to influence people and what is the difference between influencing and robbery.


The transport system is very special: all lines are unidirectional and connect exactly two stops. Buses leave the originating stop with passangers each half an hour. After reaching the destination stop they return empty to the originating stop, where they wait until the next full half an hour, e.g. X:00 or X:30, where 'X' denotes the hour. The fee for transport between two stops is given by special tables and is payable on the spot. The lines are planned in such a way, that each round trip (i.e. a journey starting and finishing at the same stop) passes through a Central Checkpoint Stop (CCS) where each passenger has to pass a thorough check including body scan.


All the ACM student members leave the CCS each morning. Each volunteer is to move to one predetermined stop to invite passengers. There are as many volunteers as stops. At the end of the day, all students travel back to CCS. You are to write a computer program that helps ACM to minimize the amount of money to pay every day for the transport of their employees.

Input
The input consists of N cases. The first line of the input contains only positive integer N. Then follow the cases. Each case begins with a line containing exactly two integers P and Q, 1 <= P,Q <= 1000000. P is the number of stops including CCS and Q the number of bus lines. Then there are Q lines, each describing one bus line. Each of the lines contains exactly three numbers - the originating stop, the destination stop and the price. The CCS is designated by number 1. Prices are positive integers the sum of which is smaller than 1000000000. You can also assume it is always possible to get from any stop to any other stop.

Output
For each case, print one line containing the minimum amount of money to be paid each day by ACM for the travel costs of its volunteers.

Sample Input
2
2 2
1 2 13
2 1 33
4 6
1 2 10
2 1 60
1 3 20
3 4 10
2 4 5
4 1 50

Sample Output
46
210

題意:給出n個點和m條邊,求起始點1到各點一個來回的最短距離之和,,
思路:先算從1點到每個點的最短路徑,然後將每個邊的起點和終點交換,在求一遍最短路徑,兩者之和就是一個來回的最短路徑。。如此大的資料,一般的Floyd、Dijkstra、Bellman ford是解決不了的,於是在網上學了一個叫spfa(佇列優化)的演算法,請點選:spfa演算法詳解 其本質就是維護一個佇列,當佇列非空的時候不斷進行鬆弛操作(就是找最短),最後當佇列為空時,最後的結果就是最短路徑。
而且這個題只能用鄰接表儲存,用Vector的話也只能用一個鄰接表,兩個鄰接表儲存會超時,,,

以下AC程式碼:
#include<stdio.h>
#include<string.h>
#define ll long long
#include<queue>
#include<vector>
#define N 1000005
#define INF 1<<30
using namespace std;
int n,m;
struct Node{
    int pre;
    int v;
    int next;
};
Node node[N];
ll vis[N];
ll dis[N];   ///起點到每個節點的最短距離
vector <Node> vt[N];
ll spfa(){
    ll i;
    memset(vis,0,sizeof(vis));
    for(i=2;i<=n;i++)
        dis[i]=INF;
    dis[1]=0;       ///起點到自身的最小距離為0

    queue<int> q;
    q.push(1);      ///將起點入佇列
    while(!q.empty()){
            int tmp=q.front(); ///取隊首元素
            q.pop();

            vis[tmp]=0;
            for(i=0;i<vt[tmp].size();i++){///遍歷與tmp節點相連的節點
                int cur_v=vt[tmp][i].v;
                int cur_next=vt[tmp][i].next;

                if(dis[cur_next]>dis[tmp]+cur_v){  ///對於和tmp相連的第i個節點,判斷加入了tmp節點後距起點的距離是否變短
                    dis[cur_next]=dis[tmp]+cur_v;
                    if(!vis[cur_next]){   ///如果佇列中沒有,加入佇列
                        q.push(cur_next);
                        vis[cur_next]=1;
                    }
                }
            }
    }

    ll sum=0;
    for(i=1;i<=n;i++)   ///計算每個點到起點最短距離的和
        sum+=dis[i];
    return sum;
}
int main(){
    int i;
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&m);
        for(i=1;i<=n;i++)
            vt[i].clear();

        for(i=1;i<=m;i++){ ///建鄰接表
            scanf("%d%d%d",&node[i].pre,&node[i].next,&node[i].v);
            Node no;
            no.next=node[i].next;
            no.v=node[i].v;
            vt[node[i].pre].push_back(no);
        }

        ll sum=spfa();

       // printf("%I64d\n",sum);
        for(i=1;i<=n;i++)
            vt[i].clear();

        for(i=1;i<=m;i++){   ///交換頂點重建鄰接表
            Node no;
            no.next=node[i].pre;
            no.v=node[i].v;
            vt[node[i].next].push_back(no);
        }

        sum+=spfa();
       printf("%I64d\n",sum);
    }
    return 0;
}


相關文章