原题:

Invitation Cards
Time Limit: 8000MS   Memory Limit: 262144K
Total Submissions: 31230   Accepted: 10366

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

题目大意:输入一个有向图,从1到除1以外所有的点,再从那些点回到1之后的费用(即路上权)的相加总和。

思路:从点1到其他点就是最典型的单源点最短路径问题,又因为最多会有1000000个点和1000000条边,因此采用SPFA无疑是最好的选择。然后考虑从其他点回到1点,这里只要将图中所有的边反向,然后再求一遍点1到其他点的费用和,最后相加两次的费用和即可。

注意点:因为点很多,所以不用邻接矩阵储存图,而用邻接表储存图。

AC代码:

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<string.h>
int q,n,m;
int u1[],v1[],w1[],first1[],next1[];
int u2[],v2[],first2[],next2[];
int dis[],t[];
bool e[];
int temp;
int tmp;
int head,tail;
long long ans1,ans2;
int main(){
int i,j; scanf("%d",&q);
for(j=;j<=q;j++){
scanf("%d%d",&n,&m); for(i=;i<=n;i++){ //图的邻接表储存
first1[i]=-;
first2[i]=-;
}
for(i=;i<=m;i++){ //分别储存两种,一个是反向后的图
scanf("%d%d%d",&u1[i],&v1[i],&w1[i]);
next1[i]=first1[u1[i]];
first1[u1[i]]=i; v2[i]=u1[i];
u2[i]=v1[i]; next2[i]=first2[u2[i]];
first2[u2[i]]=i;
} for(i=;i<=n;i++) //SPFA初始化
dis[i]=0x7fffffff;
dis[]=;
memset(e,false,sizeof(e));
e[]=true;
t[]=;
head=;
tail=; while(head!=tail){
head=(head+)%; //头指针下移
temp=t[head]; //temp为队首元素,是一个点
e[temp]=false; //队列首个元素出队
tmp=first1[temp]; //tmp表示以temp为起点的并且在邻接表中的第一条边
while(tmp!=-){ //枚举与temp相连的点
if(dis[v1[tmp]]>dis[temp]+w1[tmp]){
dis[v1[tmp]]=dis[temp]+w1[tmp]; //修改
if(!e[v1[tmp]]){
e[v1[tmp]]=true; //新元素入列
tail=(tail+)%; //队尾指针下移
t[tail]=v1[tmp];
}
}
tmp=next1[tmp]; //找其他以temp为起点的边,在邻接表中找
} }
ans1=; //求和
for(i=;i<=n;i++)
ans1+=dis[i]; //下同 for(i=;i<=n;i++)
dis[i]=0x7fffffff;
for(i=;i<=;i++) //队列清空
t[i]=;
dis[]=;
memset(e,false,sizeof(e));
e[]=true;
t[]=;
head=;
tail=; while(head!=tail){
head=(head+)%;
temp=t[head];
e[temp]=false;
tmp=first2[temp];
while(tmp!=-){
if(dis[v2[tmp]]>dis[temp]+w1[tmp]){
dis[v2[tmp]]=dis[temp]+w1[tmp];
if(!e[v2[tmp]]){
tail=(tail+)%;
e[v2[tmp]]=true;
t[tail]=v2[tmp];
}
}
tmp=next2[tmp];
}
}
ans2=;
for(i=;i<=n;i++)
ans2+=dis[i];
printf("%lld\n",ans1+ans2); //输出答案
}
return ;
}
 
 
 
 
 
 

SPFA算法(2) 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 (spfa的邻接表)

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

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

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

  4. Poj 1511 Invitation Cards(spfa)

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

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

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

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

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

  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

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

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

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

随机推荐

  1. 长大Tips的第二步

    由于期末将至的缘故,组员们对于这次项目都开始表现出了懈怠的情绪,故而这一次并没有完成许多实质性的任务,相较于上一次,此次增添了登陆以及注册的功能,说来惭愧,虽然已经学习了数据库编程,可惜自己学艺不精并 ...

  2. 108. Convert Sorted Array to Binary Search Tree (building tree with resursion)

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Fo ...

  3. python接口测试-项目实践(一) 测试需求与测试思路

    测试需求: 第三方系统提供了3个接口,需要测试前端显示的字符串里的对应数据与接口数据是否一致. 测试分层: 开发人员的设计:每周从接口取一次数据,拼接完成后保存到数据库.再从数据库取数提供接口给前端开 ...

  4. selenium使用js进行点击

    WebElement button = driver.findElement(By.xpath("/html/body/div[1]/div[3]/h2/div[2]")); Ja ...

  5. 【转】Js中Prototype、__proto__、Constructor、Object、Function关系介绍

    一    Prototype.__proto__与Object.Function关系介绍        Function.Object:Js自带的函数对象.         prototype,每一个 ...

  6. 给自己的网站加上robots.txt

    今天给自己的网站加了一个robots.txt,在网上收集整理了一些资料,给自己网站也加上了robots.txt ! 顺便给大家分享一下! 一.robots.txt是什么? robots.txt是一个纯 ...

  7. Ubuntu连接上海大学校园网(ShuWlan-1x & Shu(For All))

    1.连接Shu(For All):直接连接,打开网页后可能会自动弹出登录页面,也可能需要点击浏览器菜单栏下方的跳转按钮. 2.连接ShuWlan-1x配置注意点: 认证方式:Protected EAP ...

  8. tomcat的下载和启动

    1.下载和解压 把下载好的压缩包解压,放到想刚的目录里 看一下目录: 目录介绍: 2. 启动和访问 启动步骤: 如果startup.bat 双击执行脚本一闪而过,解决方法: 第一可能是:没有配置JAV ...

  9. Spring知识点总结(六)之Spring事务

    **************************************************************************************************** ...

  10. jmeter 填写URL链接后 不能有多余的空格。