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 ...
随机推荐
- linux top命令
top命令参数 -h:help表示显示帮助的意思 -v:version显示版本的意思,和-h的功能一样 -u:user显示指定用户的进程,例如:top -u root -p:pid显示指定进程,例如: ...
- XMLHttpRequest upload属性
一.新版本的XMLHttpRequest对象,传送数据的时候,有一个progress事件,用来返回进度信息. 它分成上传和下载两种情况 1)下载的progress事件属于XMLHttpRequest对 ...
- 如何低成本的打造HTC Vive虚拟演播室直播MR视频?
http://m.toutiao.com/i6298923859378700802/?tt_from=weixin&utm_campaign=client_share&from=gro ...
- 使用spawn-fcgi管理php-cgi
spawn-fcgi -a 127.0.0.1 -p 9000 -C 5 -u nginx -g nginx -f php-cgi
- jQuery轮播
一,简单实现轮播 //轮播容器 .carousel //轮播容器--可设宽度 (carousel="轮播") //轮播指标 .carousel-indicators ...
- Java内存分配
概述 对从事C和C++的程序员来说,在内存管理方面,他们既是拥有最高权利的人,也是从事最基础工作的“劳动人民”. 而对于Java程序员来说,JVM自动进行内存管理,程序员不再需要为每一个new操作去写 ...
- 樱花漫地集于我心,蝶舞纷飞祈愿相随 训练:a preparation 训练:a preparation
知识点: 分 ...
- SAP Adapter启动报错
在启动Adapter的时候,抛出GateWay Service Exception 当时用的gateway service是“sapgw00” 解决方法:这是由于当sap系统向本机注册服务的时候出错, ...
- JAVA 接口与抽象类的区别
abstract class 与interface都是用来定义抽象概念.例如,对于三角形.圆形.矩形这一些具体的概念,形状就是抽象的概念.因为抽象的概念在问题领域没有具体的概念,所以不能够实例化. a ...
- CSU 1325 莫比乌斯反演
题目大意: 一.有多少个有序数对(x,y)满足1<=x<=A,1<=y<=B,并且gcd(x,y)为p的一个约数: 二.有多少个有序数对(x,y)满足1<=x<=A ...