Invitation Cards
Time Limit: 8000MS   Memory Limit: 262144K
Total Submissions: 18198   Accepted: 5969

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

 
 
 
题解:由题目可知,spfa即可解决。根据spfa的特点,从源点1作spfa以后,各点的dist都是最短路径长,求和。然后求其他n-1个点到1的最短路。根据数据规模,如果作n-1次spfa肯定难以承受,单向道路反向建图,再从源点1作spfa,各点dist再求和,与原图n-1个点到1的dist之和等价。两次和相加即为所求。
       要注意和sum的范围。int sum的话结果会越界导致WA。
 
代码:
 #include<stdio.h>
#include<string.h>
#include<stdbool.h>
#include<limits.h>
int i,j,n,m,p,tot,
q[],toit[],list[],next[],cost[],dist[],
ai[],bi[],ci[];
double sum;
bool can[]; int
pre(void)
{
memset(q,,sizeof(q));
memset(toit,,sizeof(toit));
memset(next,,sizeof(next));
memset(list,,sizeof(list));
memset(cost,,sizeof(cost));
memset(can,true,sizeof(can));
memset(ai,,sizeof(ai));
memset(bi,,sizeof(bi));
memset(ci,,sizeof(ci));
sum=;tot=;
return ;
} int
add(int x,int y,int z)
{
tot++;
cost[tot]=z;
next[tot]=list[x];
list[x]=tot;
toit[tot]=y; ai[tot]=y; bi[tot]=x; ci[tot]=z;
return ;
} void
spfa(int s)
{
int i,j,head,tail,v,k;
q[]=s;
head=;tail=;
for(i=;i<=n;i++)
dist[i]=INT_MAX >> ;
dist[s]=;
can[s]=false; while(head!=tail)
{
head=head%+;
v=q[head];
k=list[v]; while(k!=)
{
if((dist[v]+cost[k])<dist[toit[k]])
{
dist[toit[k]]=dist[v]+cost[k];
if (can[toit[k]])
{
tail=tail%+;
q[tail]=toit[k];
can[toit[k]]=false;
} }
k=next[k];
}
can[v]=true;
} } int
main()
{
int x,y,z,ca;
scanf("%d\n",&p);
for(ca=;ca<=p;ca++)
{
pre();
scanf("%d%d\n",&n,&m);
for(i=;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
}
spfa(); for(i=;i<=n;i++)
sum+=dist[i]; memset(q,,sizeof(q));
memset(toit,,sizeof(toit));
memset(next,,sizeof(next));
memset(list,,sizeof(list));
memset(cost,,sizeof(cost));
memset(can,true,sizeof(can));
tot=; for(i=;i<=m;i++)
add(ai[i],bi[i],ci[i]); spfa();
for(i=;i<=n;i++)
sum+=dist[i]; printf("%.f\n",sum);
}
return ;
}
 

[POJ] 1511 Invitation Cards的更多相关文章

  1. POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / SCU 1132 Invitation Cards / ZOJ 2008 Invitation Cards / HDU 1535 (图论,最短路径)

    POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / ...

  2. POJ 1511 Invitation Cards(单源最短路,优先队列优化的Dijkstra)

    Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 16178   Accepted: 526 ...

  3. DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards

    题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. PO ...

  4. POJ 1511 Invitation Cards (spfa的邻接表)

    Invitation Cards Time Limit : 16000/8000ms (Java/Other)   Memory Limit : 524288/262144K (Java/Other) ...

  5. POJ 1511 Invitation Cards (最短路spfa)

    Invitation Cards 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/J Description In the age ...

  6. Poj 1511 Invitation Cards(spfa)

    Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 24460 Accepted: 8091 De ...

  7. (简单) POJ 1511 Invitation Cards,SPFA。

    Description In the age of television, not many people attend theater performances. Antique Comedians ...

  8. POJ 1511 Invitation Cards 链式前向星+spfa+反向建边

    Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 27200   Accepted: 902 ...

  9. poj 1511 Invitation Cards(最短路中等题)

    In the age of television, not many people attend theater performances. Antique Comedians of Malidine ...

随机推荐

  1. 最牛B的编码套路 【转】

    原文:http://blog.csdn.net/happydeer/article/details/17023229 最近,我大量阅读了Steve Yegge的文章.其中有一篇叫“Practicing ...

  2. win10系统加载ahci驱动的操作方案(Win10之家)

    win10系统使用的过程中很多用户会想要加载ahci驱动,但是大部分用户根本不知道怎么操作加载ahci驱动,这样的话很多用户会遇到一些问题,那如果使用的过程中想要加载ahci驱动的话我们应该怎么操作呢 ...

  3. java比较相等符

    public class Test1 { /** * @param args */ public static void main(String[] args){ int a = 1000, b = ...

  4. C#使用.net.mail配置163邮箱报错:不允许使用邮箱名称。 服务器响应为:authentication is required,smtp9,DcCowABHK4UYE11W2k6fAQ--.52196S2 1448940312

    client.UseDefaultCredentials = true; 要放在 client.Credentials = new NetworkCredential("用户名", ...

  5. PyInstaller打包Python脚本为exe

    1.PyInstaller-3.1.1  百度云链接  http://pan.baidu.com/s/1jHYWin8 密码  oapl 2.安装最新版本的 pywin32-217.win32-py2 ...

  6. javascript定时器:setTimeout与setInterval

    概述: setTimeout:在指定的延迟时间之后调用一个函数或者执行一个代码片段,只执行一次: setInterval:周期性地调用一个函数(function)或者执行一段代码,重复执行: 语法格式 ...

  7. dom4j解析接口使用SOAP传递的xml

    xml 文件的格式类型: <?xml version="1.0" encoding="utf-8"?> <SOAP-ENV:Envelope ...

  8. ORACLE SEQUENCE 介绍

    在oracle中sequence就是所谓的序列号,每次取的时候它会自己主动添加,一般用在须要按序列号排序的地方.  1.Create Sequence  你首先要有CREATE SEQUENCE或者C ...

  9. Composer 基本指令操作使用

    Composer 基本指令操作使用 註: 若 composer.phar 改名為 composer, 請自行將 "php composer.phar" 替換成 "comp ...

  10. springmvc4+hibernate4分页查询功能

    Springmvc+hibernate成为现在很多人用的框架整合,最近自己也在学习摸索,由于我们在开发项目中很多项目都用到列表分页功能,在此参考网上一些资料,以springmvc4+hibnerate ...