Invitation Cards (POJ-1511)

在电视时代,古希腊喜剧的传播面临挑战。ACM组织通过优化学生志愿者的交通路线,利用计算机程序最小化每日交通成本,提升戏剧宣传效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Invitation Cards (POJ-1511)

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

题目翻译:


在电视时代,很少有人去剧院演出。马里迪内西亚的古代喜剧演员们知道这个事实。他们想要宣传戏剧,尤其是古典喜剧。他们印制了载有所有必要资料和节目的邀请卡。许多学生被雇来把这些请帖分发给人们。每个学生志愿者都指定了一个公交车站,他或她会在那里呆上一整天,邀请人们乘坐公交车。他们开设了一门特殊的课程,让学生们学习如何影响他人,以及影响与抢劫之间的区别。

交通系统非常特殊:所有线路都是单向的,而且恰好连接两个站点。每半小时有一辆公共汽车从始发站开出。到达目的地站后,它们返回空的起始站,在那里它们等待到下一个完整的半小时,例如X:00或X:30,其中“X”表示时间。两站之间的交通费由专用表格支付,并在现场支付。这些线路的规划是这样的,每次往返(即在同一站出发和结束的旅程)都要经过一个中央检查站(CCS),在那里每位乘客必须通过包括身体扫描在内的全面检查。

所有的ACM学生成员每天早上离开CCS。每位志愿者都要在预定的一站下车,邀请乘客。志愿者和车站一样多。最后,所有的学生都回到CCS。你要编写一个电脑程序,帮助ACM将每天运送员工的费用降到最低。

输入

输入由N种情况组成。输入的第一行只包含正整数n。每种情况都以一行开始,其中恰好包含两个整数P和Q, 1 <= P,Q <= 1000000。P为包括CCS在内的站点数,Q为公交线路数。然后是Q线,每条线描述一条总线。每一行都包含三个数字——起始站、目的地站和价格。CCS是由1号指定的。价格是正整数,其和小于1000000000。你也可以假设从一站到另一站总是可能的。

输出

对于每个案例,打印一行包含ACM每天为其志愿者的旅费支付的最低金额。

思路:单向图,n个点m条路。问题是求从1到其它点一来一回共多少距离。

#include <algorithm>
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <queue>
#include <stack>
#include <map>
#define inf 0x3f3f3f3f
#define MAX 1000010
using namespace std;
struct node
{
    int e,v,next;
} edge[2][MAX];
long long dis[MAX],ans;
int n,m;
int vis[MAX],head[2][MAX];
void spfa(int cnt)
{
    int i,j,k;
    for(i=1; i<=n; i++)
        dis[i]=inf;
    memset(vis,0,sizeof(vis));
    queue<int>Q;
    Q.push(1);
    dis[1]=0;
    vis[1]=1;
    while(!Q.empty())
    {
        int u=Q.front();
        Q.pop();
        vis[u]=0;
        for(int i=head[cnt][u]; i!=-1; i=edge[cnt][i].next)
        {

            int b=edge[cnt][i].e;
            if(dis[u]+edge[cnt][i].v>dis[b])
                continue;
            dis[b]=dis[u]+edge[cnt][i].v;
            if(!vis[b])
            {
                Q.push(b);
                vis[b]=1;
            }
        }
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(int i=1; i<=n; i++)
        {
            head[0][i]=-1;
            head[1][i]=-1;
        }
        int a,b,c;
        for(int i=1; i<=m; i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            edge[0][i].v=c;
            edge[0][i].e=b;
            edge[0][i].next=head[0][a];
            head[0][a]=i;
            edge[1][i].v=c;
            edge[1][i].e=a;
            edge[1][i].next=head[1][b];
            head[1][b]=i;
        }
        spfa(0);
        ans=0;
        for(int i=1;i<=n;i++)
            ans+=dis[i];
        spfa(1);
        for(int i=1;i<=n;i++)
            ans+=dis[i];
        printf("%lld\n",ans);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值