题目链接: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. SSD ECC中的LDPC编解码原理

    转自:http://blog.csdn.net/zhuzongpeng/article/details/78899198 目前SSD中ECC纠错代码主要两种BCH和LDPC.不过,随着SSD对ECC纠 ...

  2. linux下面MySQL变量修改及生效

    今天在访问mysql项目的时候突然报500错误,没有找到连接,因此想到mysql的连接时间. mysql> show global variables; 主要就是连接时间是28800(8小时), ...

  3. 建立django博客应用及数据库模型

    1.现在就来创建我们的 Django 博客应用,我把它命名为 blog.激活虚拟环境,进入到 manage.py 文件所在的目录下,运行 python manage.py startapp blog ...

  4. vue之条件渲染

    一.v-if v-if指令用于条件的渲染一块内容,当指令的表达式返回true时,内容才会被渲染. <h1 v-if="isshow">要显示么</h1> d ...

  5. centos 下完全卸载 mysql5.6

    查看已经安装的服务 rpm –qa|grep -i mysql -i 作用是不区分大小写 yum remove mysql mysql-server mysql-libs compat-mysql51 ...

  6. 用C++实现约瑟夫环的问题

    约瑟夫问题是个有名的问题:N个人围成一圈.从第一个開始报数,第M个将被杀掉,最后剩下一个,其余人都将被杀掉. 比如N=6,M=5.被杀掉的人的序号为5,4,6.2.3.最后剩下1号. 假定在圈子里前K ...

  7. table合并单元格

    table合并单元格 新建一张表格.要求表格有四行四列,当中第一行的四列合并,第二行.第三行和第四行的第一列合并 <!DOCTYPE html PUBLIC "-//W3C//DTD ...

  8. Android 使用ListView的A-Z字母排序功能实现联系人模块

    在上一篇文章其中,主要学习了ListView的A-Z字母排序功能以及依据输入框的输入值改变来过滤搜索结果,假设输入框里面的值为空.更新为原来的列表,否则为过滤数据列表,包含汉字转成拼音的功能.假设你还 ...

  9. Solidworks 如何绘制投影曲线

    1 画一个半圆,然后旋转360°得到一个正圆   2 在视图中任意绘制一条平面曲线(用样条曲线绘制)   3 退出草图,在特征选项卡中点击"投影曲线"   4 将草图2(一条平面曲 ...

  10. 【iOS】系统框架学习

    iOS的系统架构分为四个层次:核心操作系统层(Core OS layer).核心服务层(Core Services layer).媒体层(Media layer)和可触摸层(Cocoa Touch l ...