POJ 1511 Invitation Cards 邻接表 spfa算法

来源:互联网 发布:辛集淘宝运营培训学校 编辑:程序博客网 时间:2024/05/22 04:49

原题: http://poj.org/problem?id=1511

题目:

Invitation Cards
Time Limit: 8000MS Memory Limit: 262144K
Total Submissions: 22255 Accepted: 7293
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到每个点去一次,去了马上回来,再去下一个点,求往返路径和。

如果只有100个点,跑一遍floyd就可以了,这里有10w个点,不行。
朴素的dijkstra是N^2的复杂度,这里要超时。
所以这里我们用spfa这种接近2N的算法。

由于二维数组空间不够,所以只能用vector或者邻接表,因为vector的适合经常修改,本身是链表结构,每次插入数据都会消耗时间来申请内存并和前面建立联系,虽然可以支持下标访问,但是效率肯定赶不上数组的下标访问,所以有时候用vector会超时,这里我们用邻接表来存。

由于我们要求原点到每个点的来回距离,求一遍spfa我们只能得到单向距离和,而返回的路怎么求呢?
先说我们再纸上画的情况,如下图例:
这里写图片描述
我们以A为起点,跑一遍spfa能求出A到每个点的距离。
现在B到A的路只有一条,是B->C->D->A,如果我们把顺序反过来看,可以发现A->D->C->B是他的回路。如果我们把整个地图的箭头都反过来,再求一次spfa,那么就能得到最后的结果了。

代码:

#include"stdio.h"#include"iostream"#include"string.h"#include"queue"using namespace std;typedef long long int lint;const lint INF = 0x3F3F3F3F3F3F3F3F;const int N= 1000005;struct node{    int en;    int len;    int next;};int n,m;bool vis[N];lint dis[N];int head1[N];int head2[N];node maze1[N];node maze2[N];void spfa1(){    memset(vis,false,sizeof(vis));    for(int i=0; i<=n; i++)    dis[i]=INF;    queue <int> q;    dis[1]=0;    vis[1]=true;    q.push(1);    while(!q.empty())    {        int x=q.front();        q.pop();        vis[x]=false;        for(int i=head1[x];i!=-1;i=maze1[i].next)        {            int en=maze1[i].en;            if(dis[en]>dis[x]+maze1[i].len)            {                dis[en]=dis[x]+maze1[i].len;                if(vis[en]==false)                {                    vis[en]=true;                    q.push(en);                }            }        }    }}void spfa2(){    memset(vis,false,sizeof(vis));    for(int i=0; i<=n; i++)        dis[i]=INF;    queue <int> q;    dis[1]=0;    vis[1]=true;    q.push(1);    while(!q.empty())    {        int x=q.front();        q.pop();        vis[x]=false;        for(int i=head2[x];i!=-1;i=maze2[i].next)        {            int en=maze2[i].en;            if(dis[en]>dis[x]+maze2[i].len)            {                dis[en]=dis[x]+maze2[i].len;                if(vis[en]==false)                {                    vis[en]=true;                    q.push(en);                }            }        }    }}int main(){    int t;    scanf("%d",&t);    while(t--)    {        memset(head1,-1,sizeof(head1));        memset(head2,-1,sizeof(head2));        scanf("%d %d",&n,&m);        for(int i=1; i<=m; i++)        {            int x,y,z;            scanf("%d %d %d",&x,&y,&z);            maze1[i].len=z;            maze1[i].en=y;            maze1[i].next=head1[x];            head1[x]=i;            maze2[i].len=z;            maze2[i].en=x;            maze2[i].next=head2[y];            head2[y]=i;        }        lint ans=0;        spfa1();         for(int i=1; i<=n; i++)        ans=ans+dis[i];        spfa2();            for(int i=1; i<=n; i++)        ans=ans+dis[i];        printf("%I64d\n",ans);    }    return 0;}
0 0