POJ 1511 Invitation Cards(最短路)

 

Invitation Cards

Time Limit: 8000MS Memory Limit: 262144K
Total Submissions: 14556 Accepted: 4710

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

Source

Central Europe 1998   本题看起来很简单。 时间也给了8s 但是点很多。 用Dijkstra和floyed都会超时。。。   对于点多边少的,用SPFA效率较高。 用两次SPFA,分别对原图和逆图求最短路径、、  

/*
POJ 1511
 题意:给出n个顶点m条边的有向图,算出从第1点到其他各个顶点的
 最短路径的和,再算出从其他各个顶点到第一个顶点的最短路径
 的和,求出两个和的和.
首先对于输入的m条边,建立一个邻接表存储起来,然后将这m条边
反向(即将终点变为起点,起点变为终点)后建立一个邻接表,在分别
用SPFA算法对这两个邻接表求最短路,算出顶点1到其他各个顶点的
最短路径,然后相加即可.


点多边少用SPFA。用Dijkstra和floyed会超时


G++ 40840K 1750ms
*/



#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
using namespace std;

const int MAXN=1000010;
const int MAXE=1000010;
const int INF=1000000009;

int head[MAXN];
bool vis[MAXN];
int que[MAXN];
int dist[MAXN];

struct Edge
{
    int to;
    int next;
    int v;
}edge[MAXE];

//逆图
int head2[MAXN];
Edge edge2[MAXE];


int tol;
void add(int a,int b,int c)
{
    edge[tol].to=b;
    edge[tol].v=c;
    edge[tol].next=head[a];
    head[a]=tol++;
}
int tol2;
void add2(int a,int b,int c)
{
    edge2[tol2].to=b;
    edge2[tol2].v=c;
    edge2[tol2].next=head2[a];
    head2[a]=tol2++;
}

void SPFA(int start,int n)//正图
{
    int front=0;
    int rear=0;
    for(int v=1;v<=n;v++)
    {
        if(v==start)
        {
            que[rear++]=v;
            vis[v]=true;
            dist[v]=0;
        }
        else
        {
            vis[v]=false;
            dist[v]=INF;
        }
    }
    while(front!=rear)
    {
        int u=que[front++];
        vis[u]=false;
        if(front>=MAXN)front=0;
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].to;
            if(dist[v]>dist[u]+edge[i].v)
            {
                dist[v]=dist[u]+edge[i].v;
                if(!vis[v])
                {
                    que[rear++]=v;
                    vis[v]=true;
                    if(rear>=MAXN)rear=0;
                }
            }
        }
    }
}

void SPFA2(int start,int n)//逆图
{
    int front=0;
    int rear=0;
    for(int v=1;v<=n;v++)
    {
        if(v==start)
        {
            que[rear++]=v;
            vis[v]=true;
            dist[v]=0;
        }
        else
        {
            vis[v]=false;
            dist[v]=INF;
        }
    }
    while(front!=rear)
    {
        int u=que[front++];
        vis[u]=false;
        if(front>=MAXN)front=0;
        for(int i=head2[u];i!=-1;i=edge2[i].next)
        {
            int v=edge2[i].to;
            if(dist[v]>dist[u]+edge2[i].v)
            {
                dist[v]=dist[u]+edge2[i].v;
                if(!vis[v])
                {
                    que[rear++]=v;
                    vis[v]=true;
                    if(rear>=MAXN)rear=0;
                }
            }
        }
    }
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int T;
    int n,m;
    int a,b,c;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        tol=tol2=0;
        memset(head,-1,sizeof(head));
        memset(head2,-1,sizeof(head2));
        while(m--)
        {
            scanf("%d%d%d",&a,&b,&c);
            add(a,b,c);
            add2(b,a,c);
        }
        long long ans=0;
        SPFA(1,n);
        for(int i=2;i<=n;i++)
          ans+=dist[i];
        SPFA2(1,n);
        for(int i=2;i<=n;i++)
          ans+=dist[i];
        printf("%I64d\n",ans);
    }
    return 0;
}

 

    原文作者:kuangbin
    原文地址: https://www.cnblogs.com/kuangbin/archive/2012/08/18/2645796.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞