D - Invitation Cards
D - Invitation Cards
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
//题目的意思是:第一行、一个整数N ,代表 N 个测试案例,第二行 ,两个整数 1<=P,Q<=一百万。意思是P个点,Q条 "有向边" 。然后就是Q行有向边和权值,求从点 1 到所有点的最小路径和,再加上从所有点到点 1 的最小路径和,数据保证所有点都能连通。
//牛逼的 spfa 算法,今天学习了spfa算法,很强大,bian数组是存放有向边并且成为链表的节点的,headlist的作用是建立链表,存放头节点的。d数组是存放到任一点最短路径长度的,vis不知道干嘛的,去掉也对,而且更快。。。我看到想了很久。。。作用是啥? 数据很大,0x f 用7个是错的,8个才对。然后逆序的就是将有向边反向,从 点1 走到所有点最小路径之和,好了,具体看代码吧。
71676kb 1907ms(去掉vis数组1797ms...)
#include <iostream>
#include <stdio.h>
#include <queue>
using namespace std; #define inf 0xffffffff
#define MAX 1000010 struct Bian
{
int e;
__int64 w;
int next;
}bian[][MAX];
__int64 d[MAX];
bool vis[MAX];
__int64 headlist[][MAX];
int n,m; void spfa(bool cap)
{
int i,x,y;
queue<int> Q;
for (i=;i<=n;i++)
{
d[i]=inf;
vis[i]=;
}
d[]=;
vis[]=;
Q.push();
while (!Q.empty())
{
x=Q.front();
Q.pop();
vis[x]=;
for (i=headlist[cap][x];i!=-;i=bian[cap][i].next)
{
y=bian[cap][i].e;
if (d[y]>d[x]+bian[cap][i].w)
{
d[y]=d[x]+bian[cap][i].w;
if (!vis[y])
{
vis[y]=;
Q.push(y);
}
}
}
}
} int main()
{
int N;
int a,b,i;
__int64 c;
__int64 ans;
scanf("%d",&N);
while (N--)
{
scanf("%d%d",&n,&m); for (i=;i<=n;i++) //初始化链表
{
headlist[][i]=-;
headlist[][i]=-;
} for (i=;i<=m;i++)
{
scanf("%d%d%I64d",&a,&b,&c);
bian[][i].e=b; //正序
bian[][i].w=c;
bian[][i].next=headlist[][a];
headlist[][a]=i; bian[][i].e=a; //逆序
bian[][i].w=c;
bian[][i].next=headlist[][b];
headlist[][b]=i;
}
ans=;
spfa();
for (i=;i<=n;i++)
ans+=d[i];
spfa();
for (i=;i<=n;i++)
ans+=d[i];
printf("%I64d\n",ans);
}
return ;
}
D - Invitation Cards的更多相关文章
- HDU 1535 Invitation Cards(最短路 spfa)
题目链接: 传送门 Invitation Cards Time Limit: 5000MS Memory Limit: 32768 K Description In the age of te ...
- 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 ...
- HDU1535——Invitation Cards(最短路径:SPAF算法+dijkstra算法)
Invitation Cards DescriptionIn the age of television, not many people attend theater performances. A ...
- Poj 1511 Invitation Cards(spfa)
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 24460 Accepted: 8091 De ...
- Invitation Cards(邻接表+逆向建图+SPFA)
Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 17538 Accepted: 5721 Description In ...
- [POJ] 1511 Invitation Cards
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 18198 Accepted: 596 ...
- poj1511/zoj2008 Invitation Cards(最短路模板题)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Invitation Cards Time Limit: 5 Seconds ...
- hdu 1535 Invitation Cards(SPFA)
Invitation Cards Time Limit : 10000/5000ms (Java/Other) Memory Limit : 65536/65536K (Java/Other) T ...
- (简单) POJ 1511 Invitation Cards,SPFA。
Description In the age of television, not many people attend theater performances. Antique Comedians ...
随机推荐
- CentOS7下挂载硬盘笔记
CentOS7下挂载硬盘笔记 准备工作 机器:DELL R730 系统:CentOS 7.4.1708 (Core) x86_64 新增硬盘:三星960PRO 关闭服务器加上新硬盘,然后重启 查看硬盘 ...
- eclipse自动添加作者、日期等注释
使用eclipse的时候一般会添加自己的注释,标注日期作者等内容,我总结的添加注释的方式有两种:一.在新建class时自动添加注释:二.通过快捷键自动添加注释.下面分别描述一下添加方式. 一.新建cl ...
- easyui datagrid 批量编辑和提交数据
easyui datagrid 行编辑和提交方,废话就不多说了,直接上代码 <div style="margin: 5px;"> <table id=" ...
- Transportation poj1040
Ruratania is just entering capitalism and is establishing new enterprising activities in many fields ...
- mybatis like写法
name like concat(concat('%',#{name}),'%') name like concat('%',#{name},'%')
- hdu4930 Fighting the Landlords(模拟 多校6)
题目链接:pid=4930">http://acm.hdu.edu.cn/showproblem.php? pid=4930 Fighting the Landlords Time L ...
- Spring使用经验之StandardServletMultipartResolver实现文件上传的基本配置
Note:Spring使用版本是4.1.6.RELEASE 1. 在实现了AbstractAnnotationConfigDispatcherServletInitializer的类中重载custom ...
- 1 android 人脸识别
1 https://www.google.com.hk/search?newwindow=1&safe=strict&q=android+%E5%9B%BE%E7%89%87%E4%B ...
- php-fpm添加service服务
原文:http://blog.csdn.net/whatday/article/details/50659992 php-fpm在5.3.2版本以后不支持service php-fpm start等命 ...
- ListView异步加载图片
ListView异步加载图片是非常实用的方法,凡是是要通过网络获取图片资源一般使用这种方法比较好,用户体验好,下面就说实现方法,先贴上主方法的代码: package cn.wangmeng.test; ...