Invitation Cards

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 2173    Accepted Submission(s): 1056

Problem 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
 
Recommend
LL   |   We have carefully selected several similar problems for you:  1317 

pid=1217" target="_blank">1217 1531 1548 1546




最短路, 先按题意建图然后求出最短路,然后建立反图求出最短路,最后把权值加起来即可了

#include <map>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int N = 1000010;
const int M = 1000010;
const int inf = 0x3f3f3f3f;
typedef pair<int, int> pi; struct node
{
int weight;
int next;
int to;
}edge[M], edge2[M]; int head[N], head2[N];
int tot1, tot2;
int dist[N]; void addedge(int from, int to, int weight)
{
edge[tot1].weight = weight;
edge[tot1].to = to;
edge[tot1].next = head[from];
head[from] = tot1++;
} void readdedge(int from, int to, int weight)
{
edge2[tot2].weight = weight;
edge2[tot2].to = to;
edge2[tot2].next = head2[from];
head2[from] = tot2++;
} void dijkstra(int v0)
{
memset ( dist, inf, sizeof(dist) );
dist[v0] = 0;
priority_queue< pi, vector<pi>, greater<pi> > qu;
while ( !qu.empty() )
{
qu.pop();
}
qu.push(make_pair( dist[v0], v0) );
while ( !qu.empty() )
{
pi tmp = qu.top();
int u = tmp.second;
int d = tmp.first;
qu.pop();
for (int i = head[u]; ~i; i = edge[i].next)
{
int v = edge[i].to;
if (dist[v] > d + edge[i].weight)
{
dist[v] = d + edge[i].weight;
qu.push( make_pair(dist[v], v) );
}
}
}
} void dijkstra2(int v0)
{
memset ( dist, inf, sizeof(dist) );
dist[v0] = 0;
priority_queue< pi, vector<pi>, greater<pi> > qu;
while ( !qu.empty() )
{
qu.pop();
}
qu.push(make_pair( dist[v0], v0) );
while ( !qu.empty() )
{
pi tmp = qu.top();
int u = tmp.second;
int d = tmp.first;
qu.pop();
for (int i = head2[u]; ~i; i = edge2[i].next)
{
int v = edge2[i].to;
if (dist[v] > d + edge2[i].weight)
{
dist[v] = d + edge2[i].weight;
qu.push( make_pair(dist[v], v) );
}
}
}
} int main()
{
int t;
int n, m, u, v, w;
scanf("%d", &t);
while (t--)
{
scanf("%d%d", &n, &m);
memset ( head, -1, sizeof(head) );
memset (head2, -1, sizeof(head2) );
tot1 = tot2 = 0;
for (int i = 0; i < m; ++i)
{
scanf("%d%d%d", &u, &v, &w);
addedge(u, v, w);
readdedge(v, u, w);
}
__int64 ans = 0;
dijkstra(1);
for (int i = 2; i <= n; ++i)
{
ans += dist[i];
}
dijkstra2(1);
for (int i = 2; i <= n; ++i)
{
ans += dist[i];
}
printf("%I64d\n", ans);
}
return 0;
}

hdu1535——Invitation Cards的更多相关文章

  1. HDU1535——Invitation Cards(最短路径:SPAF算法+dijkstra算法)

    Invitation Cards DescriptionIn the age of television, not many people attend theater performances. A ...

  2. hdu1535 Invitation Cards 最短路

    有一张图,若干人要从不同的点到同一个中间点,再返回,求总费用最小 中间点到各个点最小费用是普通的最短路 各个点到中间点最小费用其实就是将所有路径反向建边之后中间点到各个点的最小费用,同样用最短路就可以 ...

  3. 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 / ...

  4. HDU 1535 Invitation Cards(最短路 spfa)

    题目链接: 传送门 Invitation Cards Time Limit: 5000MS     Memory Limit: 32768 K Description In the age of te ...

  5. POJ 1511 Invitation Cards (spfa的邻接表)

    Invitation Cards Time Limit : 16000/8000ms (Java/Other)   Memory Limit : 524288/262144K (Java/Other) ...

  6. POJ 1511 Invitation Cards (最短路spfa)

    Invitation Cards 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/J Description In the age ...

  7. Poj 1511 Invitation Cards(spfa)

    Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 24460 Accepted: 8091 De ...

  8. Invitation Cards(邻接表+逆向建图+SPFA)

    Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 17538   Accepted: 5721 Description In ...

  9. [POJ] 1511 Invitation Cards

    Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 18198   Accepted: 596 ...

随机推荐

  1. SOAP很2的问题

    小弟说实话不很懂这方面的东西,但上头要求比较一下这两种方式的优劣,欢迎大家赐教啊!“按照一定的网络传输协议,通过符合 FTP/TCP/IP等协议的数据报或者以SOAP的方式传送到接口服务器.”这是出处 ...

  2. Number of Connected Components in an Undirected Graph -- LeetCode

    Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...

  3. xcode里面使用Memory Leaks和Instruments检测内存泄漏

    教程截图: 作为一名无证程序员,无论你多么精通Objective-C的内存管理,随着时间的推移,你也不可避免的犯内存相关的错误.但通常因为代码量太大,以至于你不可能一行一行的去排除(等你解决完,你设计 ...

  4. VMware Converter 迁移物理机到 esxi 虚拟机

    http://dngood.blog.51cto.com/446195/685082/ VMware Converter  有啥用? VMware Converter 是一款能将物理电脑系统.VMwa ...

  5. md5是哈希算法的改进加强,因为不同原始值可能hash结果一样,但md5则改善了用于验证消息完整性,不同md5值原始值也必将不一样

    md5是哈希算法的改进加强,因为不同原始值可能hash结果一样,但md5则改善了用于验证消息完整性,不同md5值原始值也必将不一样

  6. git reset revert区别

    git revert HEAD~1 撤销倒数第二次提交,并将这次操作作为一个新提交添加到log里,之前的提交历史不变,是撤销某次提交 git reset,直接回退到指定版本 git reset --s ...

  7. scala,import test._ ; import test.{ClassA,ClassB}

    在scala中,*不是通配符,下斜杠“_”才是通配符.因此当使用某个package所有的类时,直接使用:import test._:使用某几个时,直接使用:import test.{ClassA,Cl ...

  8. 为什么代理属性设置成assign为了防止生成保留环来

    循环引用 全部的引用计数系统, 都存在循环应用的问题, 比如以下的引用关系: 1. 对象a创建并引用到了对象b 2. 对象b创建并引用到了对象c 3. 对象c创建并引用到了对象b 这时候b和c的引用计 ...

  9. IO流知识点

    如何判断是输入还是输出?答:以程序为中心.如何判断是解码还是编码?答:以程序为中心.程序只懂二进制,所以,以二进制转换成字符是解码,字符转换成二进制是编码.1. 首先,File 它是给程序跟文件或文件 ...

  10. django自定义过滤器及模板标签

    创建一个模板库 不管是写自定义标签还是过滤器,第一件要做的事是创建模板库(Django能够导入的基本结构). 创建一个模板库分两步走: 第一,决定模板库应该放在哪个Django应用下. 如果你通过 m ...