SPFA算法(2) POJ 1511 Invitation Cards
原题:
Time Limit: 8000MS | Memory Limit: 262144K | |
Total Submissions: 31230 | Accepted: 10366 |
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之后的费用(即路上权)的相加总和。
思路:从点1到其他点就是最典型的单源点最短路径问题,又因为最多会有1000000个点和1000000条边,因此采用SPFA无疑是最好的选择。然后考虑从其他点回到1点,这里只要将图中所有的边反向,然后再求一遍点1到其他点的费用和,最后相加两次的费用和即可。
注意点:因为点很多,所以不用邻接矩阵储存图,而用邻接表储存图。
AC代码:
- #include<stdio.h>
- #include<stdlib.h>
- #include<stdbool.h>
- #include<string.h>
- int q,n,m;
- int u1[],v1[],w1[],first1[],next1[];
- int u2[],v2[],first2[],next2[];
- int dis[],t[];
- bool e[];
- int temp;
- int tmp;
- int head,tail;
- long long ans1,ans2;
- int main(){
- int i,j;
- scanf("%d",&q);
- for(j=;j<=q;j++){
- scanf("%d%d",&n,&m);
- for(i=;i<=n;i++){ //图的邻接表储存
- first1[i]=-;
- first2[i]=-;
- }
- for(i=;i<=m;i++){ //分别储存两种,一个是反向后的图
- scanf("%d%d%d",&u1[i],&v1[i],&w1[i]);
- next1[i]=first1[u1[i]];
- first1[u1[i]]=i;
- v2[i]=u1[i];
- u2[i]=v1[i];
- next2[i]=first2[u2[i]];
- first2[u2[i]]=i;
- }
- for(i=;i<=n;i++) //SPFA初始化
- dis[i]=0x7fffffff;
- dis[]=;
- memset(e,false,sizeof(e));
- e[]=true;
- t[]=;
- head=;
- tail=;
- while(head!=tail){
- head=(head+)%; //头指针下移
- temp=t[head]; //temp为队首元素,是一个点
- e[temp]=false; //队列首个元素出队
- tmp=first1[temp]; //tmp表示以temp为起点的并且在邻接表中的第一条边
- while(tmp!=-){ //枚举与temp相连的点
- if(dis[v1[tmp]]>dis[temp]+w1[tmp]){
- dis[v1[tmp]]=dis[temp]+w1[tmp]; //修改
- if(!e[v1[tmp]]){
- e[v1[tmp]]=true; //新元素入列
- tail=(tail+)%; //队尾指针下移
- t[tail]=v1[tmp];
- }
- }
- tmp=next1[tmp]; //找其他以temp为起点的边,在邻接表中找
- }
- }
- ans1=; //求和
- for(i=;i<=n;i++)
- ans1+=dis[i];
- //下同
- for(i=;i<=n;i++)
- dis[i]=0x7fffffff;
- for(i=;i<=;i++) //队列清空
- t[i]=;
- dis[]=;
- memset(e,false,sizeof(e));
- e[]=true;
- t[]=;
- head=;
- tail=;
- while(head!=tail){
- head=(head+)%;
- temp=t[head];
- e[temp]=false;
- tmp=first2[temp];
- while(tmp!=-){
- if(dis[v2[tmp]]>dis[temp]+w1[tmp]){
- dis[v2[tmp]]=dis[temp]+w1[tmp];
- if(!e[v2[tmp]]){
- tail=(tail+)%;
- e[v2[tmp]]=true;
- t[tail]=v2[tmp];
- }
- }
- tmp=next2[tmp];
- }
- }
- ans2=;
- for(i=;i<=n;i++)
- ans2+=dis[i];
- printf("%lld\n",ans1+ans2); //输出答案
- }
- return ;
- }
SPFA算法(2) POJ 1511 Invitation Cards的更多相关文章
- 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 (spfa的邻接表)
Invitation Cards Time Limit : 16000/8000ms (Java/Other) Memory Limit : 524288/262144K (Java/Other) ...
- 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)
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 24460 Accepted: 8091 De ...
- (简单) 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 ...
- POJ 1511 Invitation Cards(逆向思维 SPFA)
Description In the age of television, not many people attend theater performances. Antique Comedians ...
- [POJ] 1511 Invitation Cards
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 18198 Accepted: 596 ...
- POJ 1511 Invitation Cards(单源最短路,优先队列优化的Dijkstra)
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 16178 Accepted: 526 ...
随机推荐
- Python模块 Socket
一个简单的python socket编程 一.套接字 套接字是为特定网络协议(例如TCP/IP,ICMP/IP,UDP/IP等)套件对上的网络应用程序提供者提供当前可移植标准的对象.它们允许程序接受并 ...
- MSMQ学习笔记一——概述
一.MSMQ是什么 Message Queuing(MSMQ) 是微软开发的消息中间件,可应用于程序内部或程序之间的异步通信.主要的机制是:消息的发送者把自己想要发送的信息放入一个容器中(我们称之为M ...
- MATLAB安装与注册(血泪总结)
工具/原料 R2016a_win64.iso(安装文件) Matlab 2016a Win64 Crack.rar(破解文件) 方法/步骤 1 下载R2016a_win64.iso(安装文件) ...
- ORACLE常用函数汇总(持续更新中....)
在使用ORACLE过程中,把一些常用的函数的相关用法,注意事项进行简单的汇总,便于自己查询参考. DBMS_RANDOM包 dbms_random是一个可以生成随机数值或者字符串的程序包.这个包有in ...
- ACM-ICPC 2017 Asia Xi'an J LOL 【暴力 && 排列组合】
任意门:https://nanti.jisuanke.com/t/20750 J - LOL 5 friends play LOL together . Every one should BAN on ...
- 2016 ACM/ICPC亚洲区大连站-重现赛 解题报告
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=5979 按AC顺序: I - Convex Time limit 1000 ms Memory li ...
- 打开eclipse出现an error has occurred.see the loh file
解决方案: 1,打开eclipse安装目录下的eclipse.ini文件:2,打开的文本文件最后添加一行--add-modules=ALL-SYSTEM3,保存重新打开Eclipse. 测试过已经ok
- linux 使用sqlite3
:c中使用sqlite3需要调用函数接口操作: sqlite3 *db; int status=sqlite_open("dbname",&db);//打开或者创建数据库 ...
- 数据库——MySQL——单表查询
单表查询语法: SELECT 字段1,字段2... FROM 表名 WHERE 条件 GROUP BY field HAVING 筛选 ORDER BY field LIMIT 限制条数 关键字的执行 ...
- 史上最简单的 SpringCloud 教程 | 第一篇: 服务的注册与发现Eureka(Finchley版本)
转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f1-eureka/ 本文出自方志朋的博客 一.spring ...