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. sql 函数的理解

    CAST (expression AS data_type) 用于数据的转化 isnull(@s+',','select years,months,') 判断(@s+',')是否为空,注意点,即使(@ ...

  2. 读《编写可维护的JavaScript》第一章总结

    第一章 基本的格式化 1.4 ① 换行 当一行长度到达了单行最大的字符限制时,就需要手动将一行拆成俩行.通常我们会在运算符后换行,下一行会增加俩个层级的缩进. // 好的做法: 在运算符后换行,第二行 ...

  3. Java原始的8中数据类型

    数据类型 大小 范围 默认值 ========   ========  ============================================  =========byte(字节) ...

  4. js修改:before、:after的内容

    一.js控制伪元素content内容 二. --------------2016-7-20 13:34:03-- source:[1]js如何控制伪元素的内容

  5. docker部署tomcat

    一.环境简介 宿主机版本:ubuntu-14.04.3-server-amd64.iso JDK版本:jdk-7u76-linux-x64.tar.gz TOMCAT版本:apache-tomcat- ...

  6. java高薪之路__008_Annotation

    元注解 共有4种 @Retention 表示需要在什么级别保存该注释信息(生命周期) |--- RetentionPolicy.SOURCE: 停留在java源文件,编译器被丢掉 |--- Reten ...

  7. 控制反转(IOC: Inverse Of Control) & 依赖注入(DI: Independence Inject)

    举例:在每天的日常生活中,我们离不开水,电,气.在城市化之前,我们每家每户需要自己去搞定这些东西:自己挖水井取水,自己点煤油灯照明,自己上山砍柴做饭.而城市化之后,人们从这些琐事中解放了出来,城市中出 ...

  8. [ubuntu14.04 amd64 ]搜狗拼音輸入法安裝

    这个网址下载之后,双击下载的deb文件http://mirrors.sohu.com/deepin/pool/non-free/f/fcitx-sogoupinyin-release/ 就会在ubun ...

  9. kafka原理存储

    http://www.open-open.com/lib/view/open1421150566328.html

  10. Windows Store App 关键帧动画

    关键帧动画和插值动画类似,同样可以根据目标属性值的变化产生相应的动画效果,不同的是,插值动画是在两个属性值之间进行渐变,而关键帧动画打破了仅通过两个属性值控制动画的局限性,它可以在任意多个属性值之间进 ...