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

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
#include
#define ll long long
#include
#include
#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  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 q;
    q.push(1);      ///将起点入队列
    while(!q.empty()){
            int tmp=q.front(); ///取队首元素
            q.pop();

            vis[tmp]=0;
            for(i=0;idis[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;
}
 
  

你可能感兴趣的:(ACM,算法)