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 ...
随机推荐
- mysql开发之join语句学习
内连接:inner join -- 全外链接:full outer 左外连接:left outer 右外连接:right outer 交叉连接:cross内连接,两个表中重复部分全外连接,两个表所有字 ...
- Unity3d / 3ds max 模型分享站点
http://www.cgrealm.org/model/ 王国3D模型库 http://www.cgjoy.com/ 游戏特效论坛
- Java千百问_03基本的语法(005)_二进制是如何做位运算的
点击进入_很多其它_Java千百问 二进制是如何做位运算的 程序中的全部数在计算机内存中都是以二进制的形式储存的.位运算说白了,就是直接对整数在内存中的二进制位进行操作. 其它运算符看这里:java种 ...
- Word文档打不开怎么办
目前一些主流的办公软件给大家日常工作带来了很大便利,比如:Microsoft Office或金山WPS!我们在愉快地使用它们的同时,多少也遇到了一些让人尴尬或头疼的问题,比如:精心制作的文档,突然打不 ...
- python 赋值 深浅拷贝
深浅拷贝 一.数字和字符串 对于 数字 和 字符串 而言,赋值.浅拷贝和深拷贝无意义,因为其永远指向同一个内存地址. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 impor ...
- javascript - 封装ajax
封装,使用有示例. // 封装示例: function ajax(url, method, params, done) { var xhr = null; method = method.toUppe ...
- Django——静态文件(如bootstrap)的配置
静态文件如CSS, javascript(如bootstrap), 图片等文件在django中的配置官方文档写的比较模糊,自己通过实验验证后并整理如下,以防遗忘,目前只整理了关于本地开发中的设置方式, ...
- Nginx:访问第三方服务
参考资料<深入理解Nginx> Nginx可以当做一个强大的反向代理服务器,其反向代理模块是基于upstream方式实现的. upstream的使用方式 HTTP模块在处理任何一个请求时都 ...
- 51单片机 | 定时/计数器原理及结构(T0和T1)
———————————————————————————————————————————— 定时/计数器结构(T0和T1) 16位寄存器T0.T1分别由TH0.TL0和TH1.TL1四个8位计数器组成 ...
- 编辑mac系统环境变量后保存,提示没有权限用到下面这个命令
编辑的文件是vim /etc/paths :w !sudo tee % %代表当前编辑文件名 MAC:查看端口占用情况: lsof -i tcp: list open files lsof -i 用以 ...