题目链接: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. Spring-IOC源码解读2.3-BeanDefinition的注册

    在DefaultListAbleBeanFactory中通过一个HashMap持有载入的BeanDefinition信息 ,这个HashMap的定义在DefaultListAbleBeanFactor ...

  2. 手把手教你搭建DHCP服务器

    目录 DHCP实现原理 DHCP定义 DHCP分配方式 DHCP工作过程 初次登录 重新登录 更新租约 搭建DHCP服务器 实验目的 实验环境 实验步骤 实验结果 DHCP实现原理 DHCP定义 DH ...

  3. 互不侵犯King(bzoj 1087)

    Description 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子. Input 只有一行,包 ...

  4. 软件包管理器(bzoj 4196)

    Description Linux用户和OSX用户一定对软件包管理器不会陌生.通过软件包管理器,你可以通过一行命令安装某一个软件包,然后软件包管理器会帮助你从软件源下载软件包,同时自动解决所有的依赖( ...

  5. 巴厘岛的雕塑 BZOJ 4069

    巴厘岛的雕塑 题解: 题意是要求分组使每组的和按位取或的值最小 那么考虑贪心,尽量使高位为0 于是枚举位置,从最高位枚举 假设当前枚举到第l位. 令 f[i][j] 表示前 i 个数分成 j 组,满足 ...

  6. spl_autoload_register() && __autoload函数

    一.__autoload 这是一个自动加载函数,在PHP5中,当我们实例化一个未定义的类时,就会触发此函数. 在index.php中,由于没有包含test.class.php,在实例化printit时 ...

  7. js动态适配移动端font-size,单位:rem

    比如:目前我手中有一张psd图,大小为640*1010,适配苹果5的手机. 方法步骤: 1.我采用font-size=10px为640*1010手机的初始像素大小:  1rem=10px: 此时psd ...

  8. T2639 约会计划 codevs

    http://codevs.cn/problem/2639/ 题目描述 Description cc是个超级帅哥,口才又好,rp极高(这句话似乎降rp),又非常的幽默,所以很多mm都跟他关系不错.然而 ...

  9. Maven打包时过滤测试代码或指定特定的测试类(maven-surefire-plugin)

    1.过滤整个测试代码,可以直接在命令行上指定 mvn clean install -Dmaven.test.skip=true 提示:以上为举例,具体的构建阶段可以自定义,其中maven.test.s ...

  10. flask结合令牌桶算法实现上传和下载速度限制

    限流.限速: 1.针对flask的单个路由进行限流,主要场景是上传文件和下载文件的场景 2.针对整个应用进行限流,方法:利用nginx网关做限流 本文针对第一中情况,利用令牌桶算法实现: 这个方法:h ...