POJ-1511-Invitation Cards(SPFA 反向建图)

来源:互联网 发布:路由器端口号映射 编辑:程序博客网 时间:2024/06/07 13:15

Invitation Cards
Time Limit: 8000MS Memory Limit: 262144K
Total Submissions: 25160 Accepted: 8325
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

求1到各点加上各点到1的最短路之和

这次用链表模拟队列

#include<stdio.h>#include<string.h>#define maxn 1000005#define INF 20000000000//先用数组模拟队列//1到各点加上各点到1的最短路long long int u[maxn];long long int v[maxn];long long int w[maxn];long long int dis[maxn];long long int vis[maxn];long long int first[maxn];long long int next[maxn];long long int queuee[maxn];int main(){    long long int T;    scanf("%I64d",&T);    while(T--)    {        long long int N,M;        scanf("%I64d%I64d",&N,&M);        //各种初始化        for(int i=0; i<=N; i++)        {            first[i]=-1;//初始化邻接表            dis[i]=INF;//初始化距离            vis[i]=0;//初始化为未入队        }        //邻接表正向建图        for(int i=1; i<=M; i++)        {            scanf("%I64d%I64d%I64d",&u[i],&v[i],&w[i]);            next[i]=first[u[i]];            first[u[i]]=i;        }        //正向SPFA求1到各点距离        long long int sum=0;//总长度        long long int frond=0;//队列头指针        long long int behind=0;//队列尾指针        queuee[behind++]=1;        vis[1]=1;//标记结点入队        dis[1]=0;//自己到自己距离为0        while(frond<behind)        {            for(long long int k=first[queuee[frond]]; k!=-1; k=next[k])            {//                printf("****\n");//                printf("%d %d %d\n",dis[v[k]],dis[u[k]],w[k]);                if(dis[v[k]]>dis[queuee[frond]]+w[k])//如果可以松弛                {                    dis[v[k]]=dis[queuee[frond]]+w[k];//松弛                    if(vis[v[k]]==0)//如果可以入队                    {                        queuee[behind++]=v[k];                        vis[v[k]]=1;//入队并标记                    }                }            }            vis[queuee[frond]]=0;//出队并标记            frond++;        }        //统计1到各点最短距离和        for(int i=1; i<=N; i++)//            printf("%d\n",dis[i]);            sum+=dis[i];//        printf("%d\n",sum);        //再次各种初始化        for(int i=0; i<=N; i++)        {            first[i]=-1;//初始化邻接表            dis[i]=INF;//初始化距离            vis[i]=0;//初始化为未入队        }        //反向建图        for(int i=1; i<=M; i++)        {            next[i]=first[v[i]];            first[v[i]]=i;        }        //反向SPFA求各点到1的最短距离        frond=0;        behind=0;        queuee[behind++]=1;        vis[1]=1;        dis[1]=0;        while(frond<behind)        {            for(long long int k=first[queuee[frond]]; k!=-1; k=next[k])            {                if(dis[u[k]]>dis[queuee[frond]]+w[k])                {                    dis[u[k]]=dis[queuee[frond]]+w[k];                    if(vis[u[k]]==0)                    {                        queuee[behind++]=u[k];                        vis[u[k]]=1;                    }                }            }            vis[queuee[frond]]=0;            frond++;//出队并标记        }        //统计各点到1的最短距离        for(int i=1; i<=N; i++)            sum+=dis[i];        printf("%I64d\n",sum);    }    return 0;}
0 0
原创粉丝点击