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. 树莓派入门教程——使用Qt开发界面程序

    前言        Qt是一个1991年由奇趣科技开发的跨平台C++图形用户界面应用程序开发框架.它既可以开发GUI程序,也可用于开发非GUI程序,比如控制台工具和服务器.Qt是面向对象的框架,使用特 ...

  2. Java Thread Status(转)

    public static enum Thread.State  extends Enum<Thread.State>线程状态.线程可以处于下列状态之一: 1.NEW 至今尚未启动的线程的 ...

  3. Linux企业级项目实践之网络爬虫(24)——定制规则扩展为垂直爬虫

    在垂直搜索的索引建立之前,我们需要到垂直网站上抓取资源并做一定的处理.垂直搜索与通用搜索不同之处在于,通用搜索不需要理会网站哪些资源是需要的,哪些是不需要的,一并抓取并将其文本部分做索引.而垂直搜索里 ...

  4. 微软 Dynamics AX 学习步骤

    第一步:了解到AX的架构,AOT结构,了解AOT中表,窗体,类,job,菜单,菜单项的基础开发.知道代码可以写在那里,每个对象以及对象内部的具体设置.如果你不了解类,继承,这些,那么就需要找一下讲述类 ...

  5. 高效求幂取余 算法,复杂度 log(n)

    做TopCoder SRM 576 D2 L3 题目时,程序有个地方需要对一个数大量求幂并取余,导致程序运行时间很长,看了Editoral之后,发现一个超级高效的求幂并取余的算法,之前做System ...

  6. CUGBACM_Summer_Tranning 组队赛解题报告

    组队赛解题报告: CUGBACM_Summer_Tranning 6:组队赛第六场 CUGBACM_Summer_Tranning 5:组队赛第五场 CUGBACM_Summer_Tranning 4 ...

  7. Qt Mac 下软件Release 公布dmg

    1.首先当然是用Qt Creator.编译一个Release版本号的软件 注意到编译出来的大小非常小,才420KB,由于一些类库还没包括进去的原因.如今还仅仅能在你本地执行,复制到其它Mac电脑就不能 ...

  8. css_day6

  9. JavaScript获取元素样式

    原生的JavaScript获取写在标签内部的样式很简单: <div class="test" id="test" style="width:10 ...

  10. Sublime Text3 Package Control和Emmet插件安装方法

    因为初学前端,所以今天安装了Sumblime Text 3,然后就停不下来去找Package Control的安装方法. 网络上我找到并尝试过的方法有两种,我使用的是用Python代码去安装并成安装成 ...