poj1511
Invitation Cards
Time Limit: 8000MS | Memory Limit: 262144K | |
Total Submissions: 25099 | Accepted: 8297 |
Description
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
Output
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
题目大意:
起点为1号点,求1到所有点最短路径及所有点到1的最短路径来回总和
思路:
因为题目规模太大,不能用二维数组来存储点与点之间的权值,而且考虑到也会超时,所以选用spfa加邻接表的方式来做这道题
1-所有其他点的最短路径很好求,而其他点到1的的最短距离可以通过把起点与终点反向从而求出的便是其他点到1的距离了
代码如下:
#include <iostream>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxs = +;
const __int64 INF = 0xFFFFFFFF;
__int64 dist1[maxs],dist2[maxs],ans=;
struct Edge
{
int e,next,weight;
}edge1[maxs],edge2[maxs];
int head1[maxs],head2[maxs];
int n,m;
void spfa(Edge edge[],__int64 dist[],int head[maxs])
{
bool vis[maxs];
memset(vis,false,sizeof(vis));
for(int i=;i<=n;i++)
dist[i]=INF;
dist[]=;
queue<int> q;
q.push();
vis[]=true;
while(!q.empty())
{
int cur = q.front();
q.pop();
vis[cur]=false;
for(int i=head[cur];i!=-;i=edge[i].next)
{
if(dist[edge[i].e]>dist[cur]+edge[i].weight)
{
dist[edge[i].e]=dist[cur]+edge[i].weight;
if(!vis[edge[i].e])
{
q.push(edge[i].e);
vis[edge[i].e]=true;
}
} }
}
}
int main()
{
freopen("in.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
memset(head1,-,sizeof(head1));
memset(head2,-,sizeof(head2));
memset(edge1,,sizeof(edge1));
memset(edge2,,sizeof(edge2));
for(int i=;i<=m;i++)
{
int a,b,w;
scanf("%d%d%d",&a,&b,&w);
edge1[i].e=b;
edge1[i].weight=w;
edge1[i].next=head1[a];
head1[a]=i;
edge2[i].e=a;
edge2[i].weight=w;
edge2[i].next=head2[b];
head2[b]=i;
}
ans=;
spfa(edge1,dist1,head1);
spfa(edge2,dist2,head2);
for(int i=;i<=n;i++)
ans+=(dist1[i]+dist2[i]);
printf("%I64d\n",ans);
}
return ;
}
poj1511的更多相关文章
- ACM/ICPC 之 最短路-SPFA+正逆邻接表(POJ1511(ZOJ2008))
求单源最短路到其余各点,然后返回源点的总最短路长,以构造邻接表的方法不同分为两种解法. POJ1511(ZOJ2008)-Invitation Cards 改变构造邻接表的方法后,分为两种解法 解法一 ...
- POJ-1511 Invitation Cards---Dijkstra+队列优化+前向星正向反向存图
题目链接: https://vjudge.net/problem/POJ-1511 题目大意: 给定节点数n,和边数m,边是单向边. 问从1节点出发到2,3,...n 这些节点路程和从从这些节点回来到 ...
- POJ1511来回最短路
POJ1511 问你从1到其它点得所有最短路之和 与 其他点到1得所有最短路之和 得总和 思路很明确就是两次最短路,翻转一次地图就好了 一开始就是两次spfa之间处理好数据得更新管理就好 vect ...
- POJ-1511 Invitation Cards 往返最短路 邻接表 大量数据下的处理方法
题目链接:https://cn.vjudge.net/problem/POJ-1511 题意 给出一个图 求从节点1到任意节点的往返路程和 思路 没有考虑稀疏图,上手给了一个Dijsktra(按紫书上 ...
- POJ-1511(Dijkstra+优先队列优化+向前星)
Invitation Cards POJ-1511 从这道题我还是发现了很多的问题,首先就是快速输入输出,这里的ios::---这一行必须先放在main函数第一行,也就是输入最开始的前面,否则系统疯狂 ...
- POJ1511 Invitation Cards(多源单汇最短路)
边取反,从汇点跑单源最短路即可. #include<cstdio> #include<cstring> #include<queue> #include<al ...
- poj1511 最短路
题意:与poj3268一样,所有人需要从各点到一点再从一点到各点,求最短路总和. 与poj3268一样,先正向建图跑 dijkstra ,得到该点到其他所有各点的最短路,即这些人回去的最短路,再用反向 ...
- poj1511/zoj2008 Invitation Cards(最短路模板题)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Invitation Cards Time Limit: 5 Seconds ...
- poj1511,线段树扫描线面积
经典题,线段树扫描线其实类似区间更新,一般的做法是想象一根扫描线从上扫到下或者从左扫到右,本题的做法是从上扫到下 只要扫到了一根水平线,就将其更新到线段树对应区间中,区间和它的子区间是独立更新的 #i ...
- POJ-1511 Invitation Cards (单源最短路+逆向)
<题目链接> 题目大意: 有向图,求从起点1到每个点的最短路然后再回到起点1的最短路之和. 解题分析: 在求每个点到1点的最短路径时,如果仅仅只是遍历每个点,对它们每一个都进行一次最短路算 ...
随机推荐
- hive 报错 java.lang.RuntimeException: Unable to instantiate org.apache.hadoop.hive.metastore.HiveMetaStoreClient
Exception in thread "main" java.lang.RuntimeException: java.lang.RuntimeException: Unable ...
- centos6.5 设置ssh无密码登录
:关闭防火墙 vim /etc/selinux/config 把SELINUX=enforcing修改为SELINUX=disabled A机器root连接B机器root用户 (root用户登录) ...
- 【Linux】Memcached安装
Memcached概念 Memcached是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载. MemCache的工作流程如下:先检查客户端的请求数据是否在memcached中, ...
- 为程序使用内存缓存(MemoryCache)
为了程序的灵活性,可能为程序使用了XML等外部文件存储配置,但也有可能文件内容会被频繁读取,为了减少磁盘的读取次数,提高程序性能,可以将频繁读取的配置文件缓存到内存中,加速配置的读取.并且需要可以在配 ...
- MyISAM压缩表
如果表在创建并导入数据以后,不会在进行修改操作,那么这样的表或许适合采用MyISAM压缩表. 压缩表可以极大地减少磁盘空间暂用,因此也可以减少磁盘I/O,从而提升查询性能,压缩表也支持索引.
- Linux Vim替换字符串的一些方法小结
使用Linux环境 进行开发工作的程序猿经常有编辑器之争,是vim牛还是emacs棒.二者都是程序猿的开发神器,不管用好哪一个都会使你的工作事半功倍. 本文重点介绍 Vim的替换字符串方法技巧,这些方 ...
- A标签中传递的中文参数到Servlet 后台request.getParameter()接收时出现中文乱码
package util; import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletRequ ...
- Structure From Motion(二维运动图像中的三维重建)
SfM(Structure from Motion)简介 Structure from motion (SfM) is a photogrammetric range imaging techniqu ...
- (并查集 建立关系)Parity game -- POJ -1733
链接: http://poj.org/problem?id=1733 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82830#probl ...
- VS2010编译错误:是否忘记了向源中添加“#include "stdafx.h
VS2010编译错误:是否忘记了向源中添加“#include "stdafx.h 编译提示:fatal error C1010: 在查找预编译头时遇到意外的文件结尾.是否忘记了向源中添加“# ...