Invitation Cards

Time Limit : 16000/8000ms (Java/Other)   Memory Limit : 524288/262144K (Java/Other)
Total Submission(s) : 7   Accepted Submission(s) : 1
Problem 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
 

题目大意:给出n个点和n条有向边,求所有点到源点1的来回最短路之和(保证每个点都可以往返源点1)
解题思路:这个数据范围太大,明显的不能用floyd,dijstra,bellman-ford这些算法,用spfa的话也不能用邻接矩阵存,
     因为点太多了,所以采用spfa的邻接表存储搞定
     稍微有点注意的地方是,来回之和只需要将所有的边反向再从1到所有点求最短路就是他们的最短回路

AC代码:

 #include <stdio.h>
#include <string.h>
#define inf 9999999999
#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
struct node
{
int to;
int w;
int next;
};
queue <int > q;
int n,m;
node list[];
node list1[];
int vis[];
int dis[];
int h1[];
int h2[];
void spfa()
{
int i,j,u;
for (i = ; i <= n; i ++)
{
dis[i] = inf;
vis[i] = ;
}
q.push();
dis[] = ;
vis[] = ; while (!q.empty())
{
u = q.front();
q.pop();
vis[u] = ;
for (j = h1[u]; j ; j = list[j].next)
{
if (dis[list[j].to] > dis[u]+list[j].w)
{
dis[list[j].to] = dis[u]+list[j].w;
if (!vis[list[j].to])
{
q.push(list[j].to);
vis[list[j].to] = ;
}
}
}
}
}
void spfa1()
{
int i,j,u;
for (i = ; i <= n; i ++)
{
dis[i] = inf;
vis[i] = ;
}
q.push();
dis[] = ;
vis[] = ; while (!q.empty())
{
u = q.front();
q.pop();
vis[u] = ;
for (j = h2[u]; j ; j = list1[j].next)
{
if (dis[list1[j].to] > dis[u]+list1[j].w)
{
dis[list1[j].to] = dis[u]+list1[j].w;
if (!vis[list1[j].to])
{
q.push(list1[j].to);
vis[list1[j].to] = ;
}
}
}
}
}
int main ()
{
int i,j,t,u,v,w,ans;
scanf("%d",&t);
while (t --)
{
scanf("%d%d",&n,&m);
memset(h1,,sizeof(h1));
memset(h2,,sizeof(h2));
for (ans = ,i = ; i < m; i ++)
{
scanf("%d%d%d",&u,&v,&w);
node temp = {v,w,};
list[ans] = temp;
list[ans].next = h1[u];
h1[u] = ans;
temp.to = u;
list1[ans] = temp;
list1[ans].next = h2[v];
h2[v] = ans;
ans ++;
}
long long sum = ;
spfa();
for (i = ; i <= n; i ++)
sum += dis[i];
spfa1();
for (i = ; i <= n; i ++)
sum += dis[i];
printf("%lld\n",sum);
}
return ;
}

POJ 1511 Invitation Cards (spfa的邻接表)的更多相关文章

  1. Poj 1511 Invitation Cards(spfa)

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

  2. poj 1511 Invitation Cards spfa 邻接矩阵

    题目链接: http://poj.org/problem?id=1511 题目大意: 这道题目比较难理解,我读了好长时间,最后还是在队友的帮助下理解了题意,大意就是,以一为起点,求从一到其他各点的最短 ...

  3. 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 / ...

  4. POJ 1511 - Invitation Cards 邻接表 Dijkstra堆优化

    昨天的题太水了,堆优化跑的不爽,今天换了一个题,1000000个点,1000000条边= = 试一试邻接表 写的过程中遇到了一些问题,由于习惯于把数据结构封装在 struct 里,结果 int [10 ...

  5. SPFA算法(2) POJ 1511 Invitation Cards

    原题: Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 31230   Accepted: ...

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

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

  7. POJ 1511 Invitation Cards 正反SPFA

    题意:学生从A站到B站花费C元,将学生每天从‘1’号站送往其它所有站,再从所有站接回到‘1’号站,问着个过程最小花费是多少. 思路:因为数据很大所以要用SPFA,因为不仅要从1点连接各个点还要从各个点 ...

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

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

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

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

随机推荐

  1. Using User-Named Triggers in Oracle Forms

    A user-named trigger is a trigger defined in a form by the developer. User-Named triggers do not aut ...

  2. new一个数组,delete释放内存

    int *a = new int[4]; for(int i=0;i<4;i++) { a[i] = i; printf("a[%d]=%d\n", i, i); } del ...

  3. 13.KVM安装之网桥

    安装必须的几个库和软件(最好下载一个163的yum源,速度快点) $ yum -y install kvm python-virtinst libvirt tunctl bridge-utils vi ...

  4. 三种DSO(标准DSO、写优化DSO、直接更新DSO)、标准DSO覆盖合计规则

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  5. HTML表格与列表

    HTML表格 表格其实就是很多的小单元格,而这些小单元格很有次序的排列着,它们有很多行,很多列.这些很多行列组成的东西,就叫表格,表格是<table>标签来定义的.而<table&g ...

  6. TCP连接的状态与关闭方式及其对Server与Client的影响

    TCP连接的状态与关闭方式及其对Server与Client的影响 1. TCP连接的状态 首先介绍一下TCP连接建立与关闭过程中的状态.TCP连接过程是状态的转换,促使状态发生转换的因素包括用户调用. ...

  7. iOS Build Setting证书设置

    发布的用distribution debug的用development, debug是调试模式, 除非需要日志, 内部测试的时候, 才要debug模式的. release的用distribution

  8. JMS消息中间件系列[ActiveMQ](一)

    版本5.13.3的特性: 1.Supports a variety of Cross Language Clients and Protocols from Java, C, C++, C#, Rub ...

  9. 尽可能保留原有数据,建立UEFI与BIOS双启PE优盘

    尽可能保留原有数据,建立UEFI与BIOS双启PE优盘1.确保优盘或者移动硬盘有一个FAT32分区,如果没有FAT32分区,就用傲梅分区助手或者ppm转换一个现有的分区到FAT32分区0x0C,或者新 ...

  10. js模拟类

    ECMAScript6已经支持了class,但之前版本都不支持类,但是可以通过一些方法来模拟类. js中的类,既是重点,也是难点,很多时候都感觉模棱两可. 首先强调一下js中很重要的3个知识点:thi ...