poj 1511 Invitation Cards

Invitation Cards
Time Limit: 8000MS   Memory Limit: 262144K
Total Submissions: 23773   Accepted: 7832

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


首先读懂题意,要求的是从起点到各点的最短距离之和再加上各点到起点的最短距离(有向图);
各点到起点的最短距离只要将有向边的距离反转,仍可以化为起点到各点的最短距离之和;
普通的最短路算法好像是过不了的;
学习了一发spfa算法,艰难地靠着模板过了;
总结一下:
难点有二:
1、读懂题意后,对各点到起点最短距离的转化;
2、在普通的最短路算法失效后,使用更高效的最短路算法;

#include<iostream>
#include<cstdlib>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<stack>
#include<queue>
#include<iomanip>
#include<map>
#include<set>
#include<functional>
#define pi 3.14159265358979323846
using namespace std;
const long long int inf=1e10;
int n,m;
long long int ans;
long long int d[1000001];
struct Edge
{
    int t,w,next;//节点序号,权重,下一条边序号
}edge[2][1000001];
bool vis[1000001];//表示是否在队列中
int head[2][1000001];
void spfa(int num)
{
    queue<int> q;
    for(int i=1;i<=n;++i)
    {
        d[i]=inf;
        vis[i]=0;
    }
    d[1]=0;
    vis[1]=1;
    q.push(1);
    int v;
    int t;
    while(!q.empty())
    {
        v=q.front();
        q.pop();
        vis[v]=0;
        for(int i=head[num][v];i!=-1;i=edge[num][i].next)
        {
            t=edge[num][i].t;
            if(d[t]>d[v]+edge[num][i].w)
            {
                d[t]=d[v]+edge[num][i].w;
                if(!vis[t])
                {
                    vis[t]=1;
                    q.push(t);
                }
            }
        }
    }
    for(int i=1;i<=n;++i) ans+=d[i];
}
int main()
{
    int N;
    scanf("%d",&N);
    while(N--)
    {
        scanf("%d %d",&n,&m);
        for(int i=1;i<=n;++i)
        {
            head[0][i]=-1;
            head[1][i]=-1;
        }
        int ta,tb,tc;
        for(int i=1;i<=m;++i)
        {
            scanf("%d %d %d",&ta,&tb,&tc);
            edge[0][i].next=head[0][ta];
            head[0][ta]=i;
            edge[0][i].t=tb;
            edge[0][i].w=tc;
            edge[1][i].next=head[1][tb];
            head[1][tb]=i;
            edge[1][i].t=ta;
            edge[1][i].w=tc;
        }
        ans=0;
        spfa(0);
        spfa(1);
        printf("%lld\n",ans);
    }
    return 0;
}

你可能感兴趣的:(poj,图论)