POJ-1511 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
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
be paid each day by ACM for the travel costs of its volunteers.
Sample Input
Sample Output
# include<iostream>
# include<cstdio>
# include<cstring>
# include<queue>
# include<algorithm>
using namespace std;
const int N=;
const long long INF=<<;
struct edge
{
int to,w,nxt;
};
edge e[N+];
int n,cnt,head[N+],a1[N+],a2[N+],c[N+];
long long dis[N+];
void add(int u,int v,int w)
{
e[cnt].to=v;
e[cnt].w=w;
e[cnt].nxt=head[u];
head[u]=cnt++;
}
long long spfa()
{
fill(dis,dis+n+,INF);
queue<int>q;
q.push();
dis[]=;
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u];i!=-;i=e[i].nxt){
if(dis[e[i].to]>dis[u]+e[i].w){
dis[e[i].to]=dis[u]+e[i].w;
q.push(e[i].to);
}
}
}
long long res=;
for(int i=;i<=n;++i)
res+=dis[i];
return res;
}
int main()
{
int T,m;
scanf("%d",&T);
while(T--)
{
cnt=;
scanf("%d%d",&n,&m);
fill(head,head+n+,-);
for(int i=;i<m;++i){
scanf("%d%d%d",&a1[i],&a2[i],&c[i]);
add(a1[i],a2[i],c[i]);
}
long long ans=spfa();
//cout<<ans<<endl;
cnt=;
fill(head,head+n+,-);
for(int i=;i<m;++i)
add(a2[i],a1[i],c[i]);
ans+=spfa();
printf("%lld\n",ans);
}
return ;
}
POJ-1511 Invitation Cards (双向单源最短路)的更多相关文章
- POJ 1511 Invitation Cards ( 双向单源最短路 || 最小来回花费 )
题意 : 给出 P 个顶点以及 Q 条有向边,求第一个点到其他各点距离之和+其他各点到第一个点的距离之和的最小值 分析 : 不难看出 min( 第一个点到其他各点距离之和+其他各点到第一个点的距离之和 ...
- POJ 1511 Invitation Cards(单源最短路,优先队列优化的Dijkstra)
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 16178 Accepted: 526 ...
- POJ-1511 Invitation Cards (单源最短路+逆向)
<题目链接> 题目大意: 有向图,求从起点1到每个点的最短路然后再回到起点1的最短路之和. 解题分析: 在求每个点到1点的最短路径时,如果仅仅只是遍历每个点,对它们每一个都进行一次最短路算 ...
- 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 (最短路)
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 33435 Accepted: 111 ...
- Invitation Cards POJ - 1511 (双向单源最短路)
In the age of television, not many people attend theater performances. Antique Comedians of Malidine ...
- [POJ] 1511 Invitation Cards
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 18198 Accepted: 596 ...
- Poj 1511 Invitation Cards(spfa)
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 24460 Accepted: 8091 De ...
- SPFA算法(2) POJ 1511 Invitation Cards
原题: Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 31230 Accepted: ...
随机推荐
- bzoj1056/1862 [Zjoi2006]GameZ游戏排名系统
题目链接:1,2 treap恶心题,不多说 #include<algorithm> #include<iostream> #include<cstdlib> #in ...
- Python之路----递归函数
1.小练一下 用map来处理字符串列表,把列表中所有人都变成sb,比方alex_sb name=['alex','wupeiqi','yuanhao','nezha'] # def func(item ...
- MySQL Crash Course #07# Chapter 15. 关系数据库. INNER JOIN. VS. nested subquery
索引 理解相关表. foreign key JOIN 与保持参照完整性 关于JOIN 的一些建议,子查询 VS. 联表查询 我发现MySQL 的官方文档里是有教程的. SQL Tutorial - W ...
- 记一次ping: unknown host错误
虚拟机上一台主机,之前一直在用,可以通过xshell连接,但是忽然发现ping百度失败了! [root@mgt02 ~]# ping www.baidu.com ping: unknown host ...
- Hive-查询结果导入到 MySQL
step1:add jar /home/chenweidong/lib/hive-contrib-2.1.1-cdh.0.0.jar;add jar /home/chenweidong/lib/mys ...
- MS08_067漏洞测试——20145301
MS08_067漏洞测试 实验步骤 search MS08_067查看相关信息 show payloads命令查找需要的攻击载荷 选择generic/shell_reverse_tcp来获取漏洞主机的 ...
- 20145324王嘉澜《网络对抗技术》MSF基础应用
实践目标 •掌握metasploit的基本应用方式 •掌握常用的三种攻击方式的思路. 实验要求 •一个主动攻击,如ms08_067 •一个针对浏览器的攻击,如ms11_050 •一个针对客户端的攻击, ...
- Spark样本类与模式匹配
一.前言 样本类(case class)与模式匹配(pattern matching)是Scala中一个比较复杂的概念,往往让人感觉深陷泥沼.我在这里对Scala中的样本类与模式匹配进行了一些整理,希 ...
- Android 自动化测试介绍
1 介绍: 风格: 3, 4,
- GNU C 中零长度的数组【转】
原文链接:http://www.cnblogs.com/dolphin0520/p/3752492.html 在标准C和C++中,长度为0的数组是被禁止使用的.不过在GNU C中,存在一个非常奇怪的用 ...