D - Invitation Cards

Time Limit:8000MS     Memory Limit:262144KB     64bit IO Format:%lld & %llu

Description

In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia are aware of this fact. They want to propagate theater and, most of all, Antique Comedies. They have printed invitation cards with all the necessary information and with the programme. A lot of students were hired to distribute these invitations among the people. Each student volunteer has assigned exactly one bus stop and he or she stays there the whole day and gives invitation to people travelling by bus. A special course was taken where students learned how to influence people and what is the difference between influencing and robbery.

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

The input consists of N cases. The first line of the 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.

Output

For each case, print one line containing the minimum amount of money to be paid each day by ACM for the travel costs of its volunteers.

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的更多相关文章

  1. HDU 1535 Invitation Cards(最短路 spfa)

    题目链接: 传送门 Invitation Cards Time Limit: 5000MS     Memory Limit: 32768 K Description In the age of te ...

  2. POJ 1511 Invitation Cards (spfa的邻接表)

    Invitation Cards Time Limit : 16000/8000ms (Java/Other)   Memory Limit : 524288/262144K (Java/Other) ...

  3. POJ 1511 Invitation Cards (最短路spfa)

    Invitation Cards 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/J Description In the age ...

  4. HDU1535——Invitation Cards(最短路径:SPAF算法+dijkstra算法)

    Invitation Cards DescriptionIn the age of television, not many people attend theater performances. A ...

  5. Poj 1511 Invitation Cards(spfa)

    Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 24460 Accepted: 8091 De ...

  6. Invitation Cards(邻接表+逆向建图+SPFA)

    Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 17538   Accepted: 5721 Description In ...

  7. [POJ] 1511 Invitation Cards

    Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 18198   Accepted: 596 ...

  8. poj1511/zoj2008 Invitation Cards(最短路模板题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Invitation Cards Time Limit: 5 Seconds    ...

  9. hdu 1535 Invitation Cards(SPFA)

    Invitation Cards Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 65536/65536K (Java/Other) T ...

  10. (简单) POJ 1511 Invitation Cards,SPFA。

    Description In the age of television, not many people attend theater performances. Antique Comedians ...

随机推荐

  1. 在CcentOS系统上将deb包转换为rpm包

    deb文件格式本是ubuntu/debian系统下的安装文件,那么我想要在redhat/centos/fedora中安装,需要把deb格式的软件包转化成rpm格式. 需要用到的转换工具:alien_8 ...

  2. Apache Beam WordCount编程实战及源代码解读

    概述:Apache Beam WordCount编程实战及源代码解读,并通过intellij IDEA和terminal两种方式调试执行WordCount程序,Apache Beam对大数据的批处理和 ...

  3. Mac安装pstree

    brew install pstree pstree(选项) -a:显示每个程序的完整指令,包含路径,参数或是常驻服务的标示: -c:不使用精简标示法: -G:使用VT100终端机的列绘图字符: -h ...

  4. [转]js模块化编程之彻底弄懂CommonJS和AMD/CMD!

    原文: https://www.cnblogs.com/chenguangliang/p/5856701.html ------------------------------------------ ...

  5. mysql二进制安装及基础操作

    mysql二进制安装及基础操作 环境说明: 系统版本    CentOS 6.9 x86_64 软件版本    mysql-5.6.36-linux-glibc2.5-x86_64 1.安装 采用二进 ...

  6. idea 热部署

  7. 倍福TwinCAT(贝福Beckhoff)基础教程2.2 TwinCAT常见类型使用和转换_字符串

    一般声明字符串都会加一个长度的限制,比如A:STRING(80);至于真实的字符串长度不要超过这个限制即可   在测试中,我演示了两个字符串的方法,CONCAT字符串拼接和REPLACE字符串替换.拼 ...

  8. java笔记之static&final&abstract

    知识需要不断回顾和重新认识 一:static static类型变量初始值只能被赋值一次,它的整个生命周期是源程序,程序结束前变量都不会被释放. 例如: for(int i = 0; i<10; ...

  9. CentOs yum源安装 nginx

    1 更新源 [root@server ~]#rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-4.no ...

  10. layer 不居中的坑爹问题

    使用了该代码弹出一个图片.但居然不居中 var layer_index = layer.open({ type: 1, title: false, closeBtn: 0, area: '516px' ...