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. log4j2的使用

    预备知识 日志级别:log4j默认六个级别,即trace.debug.info.warn.error.fatal ,对应意味着该消息为追踪.调试.普通信息.警告.错误.严重错误.可以根据需要子定义其他 ...

  2. centos 7

    vmlinuz initrd=initrd.img linux dd quiet vmlinuz initrd=initrd.img inst.stage2=hd:/dev/sdb4 quiet 关I ...

  3. <a>每次点击都会让浏览器重新打开一个窗口问题

    <a> 标签的 target 属性规定在何处打开链接文档.如果在一个 <a> 标签内包含一个 target 属性,浏览器将会载入和显示用这个标签的 href 属性命名的.名称与 ...

  4. 转:已知2个整形数据a,b.不使用if,?:以及其他任何条件判断的语法,找出a跟b中数据的大者。

    答案: int max(int a,int b){return (a+b+abs(a-b))/2;} 类似的 请定义一个宏,比较两个数a.b的大小,不能使用大于.小于.if语句 答案: #define ...

  5. Spring <bean> 参数意义

    1.id bean的唯一标识, 2.class 类的完全限定名, 3.parent 父类bean定义的名字. 如果没有任何声明,会使用父bean,但是也可以重写父类.重写父类时,子bean 必须与父b ...

  6. python窗体——pyqt初体验

    连续两周留作业要写ftp的作业,从第一周就想实现一个窗体版本的,但是时间实在太短,qt零基础选手表示压力很大,幸好又延长了一周时间,所以也就有了今天这篇文章...只是为了介绍一些速成的方法,还有初学者 ...

  7. Windows Store App 旋转特效

    使用Projection类可以实现界面元素的三维效果,它可以使界面上的元素在三维空间中沿着X轴.Y轴或者Z轴旋转一定的角度,在透视转换中此类又称为元素的Projection属性,用于对元素使用3D特效 ...

  8. cookie包含中文导致的问题

    周五项目测试完毕没问题之后上线,上线之后发现有的账户登录不上 原因为,用来记录追踪用户的cookie中包含cookie.读取,写入时候发生异常. 异常大概是这个样子: java.lang.Illega ...

  9. 荣品RP4412开发板摄像头驱动调用及对焦控制

    1.关于更换不同摄像头驱动调用问题. 问:RP4412开发板,我用的摄像头640*480图像预览时OK的,但是我调用1280*720的初始化预览,摄像头没有图像了,是不是camera程序也需要修改? ...

  10. 前端获取url参数

    function GetQueryString(name){ var reg = new RegExp("(^|&)"+ name +"=([^&]*)( ...