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. Linux企业级项目实践之网络爬虫(10)——处理HTTP状态码

    HTTP状态码(HTTP Status Code)是用以表示网页服务器HTTP响应状态的3位数字代码.所有状态码的第一个数字代表了响应的五种状态之一.他们分别是:消息(1字头)成功(2字头)这一类型的 ...

  2. libeXosip2(2-3) -- eXosip2 event API

    eXosip2 event API General purpose API. Data Structures struct   eXosip_event Enumerations enum   eXo ...

  3. 法爱格2014 春夏新款欧美纯色修身高腰无袖吊带V领 拼接性感 连衣裙 黑色 M【图片 价格 品牌 报价】-京东

    法爱格2014 春夏新款欧美纯色修身高腰无袖吊带V领 拼接性感 连衣裙 黑色 M[图片 价格 品牌 报价]-京东 法爱格2014 春夏新款欧美纯色修身高腰无袖吊带V领 拼接性感 连衣裙 黑色 M

  4. day57:00:26:34

    今天开始用博客记录倒计时,也只是为了看看今天做了什么.这也是我第一用博客园记录考研生活了 倒计时57天,我在想每天花时间在这记录生活会不会浪费复习的时间,其实不会的了,不去看微博,少刷新闻....仔细 ...

  5. 在Spring aop中的propagation的7种配置的意思

    <tx:method name="find*" read-only="true" propagation ="NOT_SUPPORTED&quo ...

  6. IOS uitableviewcell 向左滑动删除编辑等

    主要实现这个方法就好了 -(NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActions ...

  7. HTML5新增的一些属性和功能之六——拖拽事件

    拖放事件的前提是分为源对象和目标对象,你鼠标拖着的是源对象,你要放置的位置是目标对象,区分这两个对象是因为HTML5的拖放事件对两者是不同的. 被拖动的源对象可以触发的事件: 1).ondragsta ...

  8. PHP设计模式笔记五:策略模式 -- Rango韩老师 http://www.imooc.com/learn/236

    策略模式 1.概述:策略模式,将一组特定的行为和算法封装成类,以适应某些特定的上下文环境,这种模式称为策略模式 例如:一个电商网站系统,针对男性女性用户要各自跳转到不同的商品类目,并且所有广告位展示不 ...

  9. Linux中图形界面和文本模式相互切换

    1.默认开机进入文本模式 如果想让开机自动进纯文本模式, 修改/etc/inittab 找到其中的 id:5:initdefault: 这行指示启动时的运行级是5,也就是图形模式 改成3就是文本模式了 ...

  10. 搭建LAMP架构

    1. 为什么下载源码包需要到官网上去下载?简单说就是为了安全,如果是非官方下载的源码包,有可能被别有用心的人动过手脚. 2. 64位机器是否可以安装32位rpm包?64位机器是否可以安装32位的mys ...