题目链接:http://poj.org/problem?id=1511

Invitation Cards
Time Limit: 8000MS   Memory Limit: 262144K
Total Submissions: 29286   Accepted: 9788

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

Source

 
 
 
题解:
1.可知最短路分为两段, 点1到各个点的最短距离,以及各个点到点1的最短距离。
2.先求出点1到各个点的最短距离, 记为dis1。
3.将各条边的方向取反, 然后再求出点1到各个点的最短距离,记为dis2。因为边取反了,所以dis2实际是各个点到点1的最短距离。
4.对(dis1+dis2)求和,即为答案。
5.同样的题型:POJ3268
 
 
 
代码如下:
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e6+; int n, m; struct edge
{
int from, to, w, next;
}edge[MAXN*];
int cnt, head[MAXN]; void add(int u, int v, int w)
{
edge[cnt].from = u;
edge[cnt].to = v;
edge[cnt].w = w;
edge[cnt].next = head[u];
head[u] = cnt++;
} void init()
{
cnt = ;
memset(head, -, sizeof(head));
} LL dis1[MAXN], dis2[MAXN];
int times[MAXN], inq[MAXN];
void spfa(int st, LL dis[])
{
memset(inq, , sizeof(inq));
memset(times, , sizeof(times));
for(int i = ; i<=n; i++)
dis[i] = LNF; queue<int>Q;
Q.push(st);
inq[st] = ;
dis[st] = ;
while(!Q.empty())
{
int u = Q.front();
Q.pop(); inq[u] = ;
for(int i = head[u]; i!=-; i = edge[i].next)
{
int v = edge[i].to;
if(dis[v]>1LL*dis[u]+1LL*edge[i].w)
{
dis[v] = 1LL*dis[u]+1LL*edge[i].w;
if(!inq[v])
{
Q.push(v);
inq[v] = ;
}
}
}
}
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
init();
scanf("%d%d", &n, &m);
for(int i = ; i<m; i++)
{
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
add(u,v,w);
} spfa(, dis1);
init();
/**一开始想直接取反,即swap(edge[i].from, edge[i].to),
后来发现处理不了head[]数组, 所以还是重新建边 */
for(int i = ; i<m; i++) //m为原来的边数, 即cnt
add(edge[i].to, edge[i].from, edge[i].w);
spfa(, dis2); LL ans = ;
for(int i = ; i<=n; i++)
ans += dis1[i]+dis2[i]; printf("%lld\n", ans);
}
}

POJ1511 Invitation Cards —— 最短路spfa的更多相关文章

  1. POJ-1511 Invitation Cards( 最短路,spfa )

    题目链接:http://poj.org/problem?id=1511 Description In the age of television, not many people attend the ...

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

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

  3. POJ-1511 Invitation Cards (双向单源最短路)

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

  4. POJ1511:Invitation Cards(最短路)

    Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 34743   Accepted: 114 ...

  5. HDU 1535 Invitation Cards (最短路)

    题目链接 Problem Description In the age of television, not many people attend theater performances. Anti ...

  6. J - Invitation Cards 最短路

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

  7. POJ1511 Invitation Cards(多源单汇最短路)

    边取反,从汇点跑单源最短路即可. #include<cstdio> #include<cstring> #include<queue> #include<al ...

  8. POJ-1511 Invitation Cards 往返最短路 邻接表 大量数据下的处理方法

    题目链接:https://cn.vjudge.net/problem/POJ-1511 题意 给出一个图 求从节点1到任意节点的往返路程和 思路 没有考虑稀疏图,上手给了一个Dijsktra(按紫书上 ...

  9. POJ-1511 Invitation Cards (单源最短路+逆向)

    <题目链接> 题目大意: 有向图,求从起点1到每个点的最短路然后再回到起点1的最短路之和. 解题分析: 在求每个点到1点的最短路径时,如果仅仅只是遍历每个点,对它们每一个都进行一次最短路算 ...

随机推荐

  1. angular中ng-repeat去重

    [html] view plain copy print?在CODE上查看代码片派生到我的代码片 <div ng-app="myApp" ng-controller=&quo ...

  2. Codevs 1299 切水果 水一发

    时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description 简单的说,一共N个水果排成一排,切M次,每次切[L,R]区间的所有水果(可能有的 ...

  3. Mac OS X 10.10.5 中 VirtualBox 5.0 里的 Win7 虚拟机无法使用 USB 3.0 设备的解决办法(补充说明)

    上一篇文章中,我说到了如何在Mac OS X 10.10.5 中让 VirtualBox 5.0 里的 Win7 虚拟机使用 USB 3.0.最近碰巧升级 MacBook Pro 为最新的 15 吋 ...

  4. centos升级glibc(升级到 2.17版)

    https://blog.csdn.net/wyl9527/article/details/78256066

  5. VMware虚拟机下安装hadoop1.x

    这是Hadoop学习全程记录第1篇,在这篇里我将介绍一下如何在Linux下安装Hadoop1.x. 先说明一下我的开发环境: 虚拟机:VMware8.0: 操作系统:CentOS6.4: 版本:jdk ...

  6. Java8 ChronoUnits枚举

    原文:http://www.yiibai.com/java8/java8_chronounits.html java.time.temporal.ChronoUnit 枚举在 Java8 中添加,以取 ...

  7. 转:Twemproxy——针对MemCached与Redis的代理

    转自: http://www.infoq.com/cn/news/2012/12/twemproxy Twemproxy是一个代理服务器,可以通过它减少Memcached或Redis服务器所打开的连接 ...

  8. linux系统之shell编程-正則表達式

    shell编程正則表達式: 1:元字符   [ ]  .   *  ? + ( )  |  {  }  ^  $ 2 : [a-z0-9]  表示匹配随意数字和字母的一个 3 :  [^a-z]    ...

  9. Maven实现Web应用集成測试自己主动化 -- 測试自己主动化(WebTest Maven Plugin)

    近期在appfuse看到使用webtest-maven-plugin实现Web应用的集成測试,研究了下.感觉很不错.对于Web应用自己主动构建很有帮助,在性能測试之前能够保证Web应用的基本功能工作正 ...

  10. Dos 改动IP 地址

    1.改动 ip地址 子网掩码 默认网关 netsh interface ip set address "本地连接" static 192.168.1.23 255.255.255. ...