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. python xml与字典的相互转换

    def trans_xml_to_dict(xml): """ 将微信支付交互返回的 XML 格式数据转化为 Python Dict 对象 :param xml: 原始 ...

  2. log4j教程 3、架构

    Log4j API设计为分层结构,其中每一层提供了不同的对象,对象执行不同的任务.这使得设计灵活,根据将来需要来扩展. 有两种类型可用在Log4j的框架对象. 核心对象: 框架的强制对象和框架的使用. ...

  3. [转载]Lenovo E431 装Centos7无线驱动安装

    FROM:http://huangyandong.blog.51cto.com/1396940/1613096 查看无线网卡型号 lspci |grep Network    #为BCM43142网卡 ...

  4. 2017.4.18 linux中执行某文件提示权限不够

    因为没有对start.sh文件的执行权限,所以提示权限不够. 加一个执行权限: chmod +x start.sh 可以看到,执行权限已经有了.此时再执行,就ok了.  

  5. 一个对比各种开源库的网站 libhunt

    https://www.libhunt.com/ https://github.com/LibHunt/awesome-javascript

  6. 甲骨文Java Archive

    甲骨文Java Archive 甲骨文Java Archive提供自助下载访问我们的一些历史的Java版本. 警告: 这些旧版本的JRE和JDK来帮助开发人员提供了在旧系统调试问题. 他们没有更新最新 ...

  7. 10. 修改端口号【从零开始学Spring Boot】

    转载:http://blog.csdn.net/linxingliang/article/details/51637017 spring boot 默认端口是8080,如果想要进行更改的话,只需要修改 ...

  8. htmlspecialchars_decode

    htmlspecialchars_decode   htmlspecialchars_decode - 将特殊的 HTML 实体转换回普通字符 htmlspecialchars - 将特殊字符转换为 ...

  9. javascript 数组 find

    find() 方法返回通过测试(函数内判断)的数组的第一个元素的值. let arr = [1,2,3,4] console.log(arr.find(i => {return i>1}) ...

  10. Android笔记---使用HttpClient发送POST和GET请求

    在Android上发送 HTTP 请求的方式一般有两种, HttpURLConnection 和 HttpClient,关于HttpURLConnection的使用方法能够參考HTTP之利用HttpU ...