POJ 1511 Invitation Cards (spfa的邻接表)
Invitation Cards
Time Limit : 16000/8000ms (Java/Other) Memory Limit : 524288/262144K (Java/Other)
Total Submission(s) : 7 Accepted Submission(s) : 1
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 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.
amount of money to be paid each day by ACM for the travel costs of its
volunteers.
题目大意:给出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的邻接表)的更多相关文章
- Poj 1511 Invitation Cards(spfa)
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 24460 Accepted: 8091 De ...
- poj 1511 Invitation Cards spfa 邻接矩阵
题目链接: http://poj.org/problem?id=1511 题目大意: 这道题目比较难理解,我读了好长时间,最后还是在队友的帮助下理解了题意,大意就是,以一为起点,求从一到其他各点的最短 ...
- 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 / ...
- POJ 1511 - Invitation Cards 邻接表 Dijkstra堆优化
昨天的题太水了,堆优化跑的不爽,今天换了一个题,1000000个点,1000000条边= = 试一试邻接表 写的过程中遇到了一些问题,由于习惯于把数据结构封装在 struct 里,结果 int [10 ...
- SPFA算法(2) POJ 1511 Invitation Cards
原题: Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 31230 Accepted: ...
- POJ 1511 Invitation Cards (最短路spfa)
Invitation Cards 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/J Description In the age ...
- POJ 1511 Invitation Cards 正反SPFA
题意:学生从A站到B站花费C元,将学生每天从‘1’号站送往其它所有站,再从所有站接回到‘1’号站,问着个过程最小花费是多少. 思路:因为数据很大所以要用SPFA,因为不仅要从1点连接各个点还要从各个点 ...
- (简单) POJ 1511 Invitation Cards,SPFA。
Description In the age of television, not many people attend theater performances. Antique Comedians ...
- POJ 1511 Invitation Cards 链式前向星+spfa+反向建边
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 27200 Accepted: 902 ...
随机推荐
- C语言面试题(三)
这篇主要聚焦在排序算法,包括常见的选择排序,插入排序,冒泡排序,快速排序.会对这四种排序的时间复杂度和空间复杂度进行探究. a.选择排序 int main(int argc,char **argv){ ...
- Reapter合并行
html文件: <asp:Repeater ID="rptEmployee" runat="server"> <HeaderTemplate& ...
- 关于H5中自定义属性的设置和获取
自定义数据属性是在HTML5中新加入的一个特性.简单来说,自定义数据属性规范规定任何以data-开头属性名并且赋值.自定义数据属性是为了保存页面或者应用程序的私有自定义数据,这些自定义数据属性保存进D ...
- 解决本机安装多版本jdk导致The type java.lang.Object cannot be resolved It is indirectly referenced ...
本机开始安装了jdk1.6,然后安装了jdk1.8 当在调自动化的时候,发现传入函数传参String类型,报错The type java.lang.Object cannot be resolved ...
- (AS3)关于arguments
一.官方说明 点击访问 二.使用心得 arguments包含了当前执行方法的参数,注意,不包含默认参数! arguments可以全局访问,可以在任何方法里访问,除此之外,在定义变量的时候或者初始化的时 ...
- Google API在线生成二维码的方法
1.先看一个实例,是用Google API生成西部e网的网站地址www.weste.net二维码的方法: http://chart.apis.google.com/chart?cht=qr&c ...
- 二分图最大权最小权完美匹配模板KM
在网上找了一份挺好的模板,先标一下哦~链接君:http://blog.csdn.net/abcjennifer/article/details/5844579 #include <iostrea ...
- centos7.0 64位系统安装 nginx
1 下载nginx 从nginx官网 http://nginx.org/ 下载新的稳定版本nginx 并上传到linux服务器 2 安装nginx 所需要的扩展 yum -y install pcre ...
- DataTable 导到Excel
/// <summary> /// 将DataTalbe导出到Excel中 /// </summary> /// <param name="dt"&g ...
- IT公司100题-27-跳台阶问题
问题描述: 一个台阶总共有n阶,一次可以跳1级或者2级.求总共有多少种跳法. 分析: 用f(n)表示n阶台阶总共有多少种跳法.n阶台阶,第一可以选择跳1阶或者2阶,则f(n) = f(n-1) + ...