hdu1535——Invitation Cards

开发者福利!热门AI工具限时免费用 购周边即赠Coding Plan Lite,Claude Code、Cursor等20+工具畅享,效率翻倍! 阅读详情

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  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;
}


有向图的spfa模板 spfa模板,双向图的,但是里面也有介绍改成单向图的。 实际是freepascal的,但是没有分类,只好放到C/C++分类里了,希望能帮到大家 立即下载

相关推荐

SPFA: Invitation Cards

G - Invitation Cards Time Limit:8000MS    Memory Limit:262144KB    64bit IO Format:%I64d & %I64u SubmitStatus Description In the age of television, not many people attend theater performance

lethic的专栏 502

hpu18级第一次训练赛

D - Wave 题目链接:https://vjudge.net/contest/341543#problem/D 题意:定义 波为 至少有两个元素的字符串的奇数位和偶数位分别是相同的,而且奇偶位不相同,求给定序列的子序列是波的最大长度 思路:开一个二维dp数组,dp [ i ] [ j ] 表示奇数位是i 偶数位是j的波的长度,全部初始化为0; 遍历一遍字符串,假设当前字符是a,这个字符可以组成...

qq_45288870的博客 260

2019中国大学生程序设计竞赛(CCPC) - 网络选拔赛-Shuffle Card

Shuffle Card Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 507Accepted Submission(s): 257 题目链接:http://acm.hdu.edu.cn/showproblem.ph...

龍丶逸的博客 3万+

【最短路】 HDOJ 1535 Invitation Cards

建正图,反图。然后用dijstra搜两边就行了。。 #include #include #include #include #include #include #include #include #include #include #include #include #include #define maxn 1000005 #d

yysys 553

Invitation Cards(SPFA+前向星反向建图)

题意:一堆人从1出发,到其他的点,然后从其他的点再回来,最短距离。和之前那个牛聚会题目一样,两次spfa。Silver Cow Party 由于这题的数据已经不能用邻接矩阵来储存,所以矩阵逆置就不能用了,所以需要用前向星进行反向建图,这里用到的也是一个很巧妙地方法。 我们需要在用结构体存边的信息的时候,多定一个边的起点信息,用来后面的反向建图。 #include&lt;iostream&gt; #...

C'est la vie! 275

HDU - 1535 Invitation Cards (最短路dijkstra)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1535点击打开链接 Invitation Cards Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 4465    Acce

xuejye的博客 366

HDU 1535 Invitation Cards

传送门 最短路。 这题瞎扯一大堆废话,其实就是一个有向图,先求从1到其他点的最短路,再求从其他点到1的最短路,上述2(n-1)个最短路的值的和作为答案(题目保证是强连通图)。 从其他点到1的最短路,我们不可能把其他每个点都作为起点运行一下,肯定会超时的。这就要用到反向建图。在正向图中某个点到1的最短路就相当于在反向图中1到这个点的最短路。(反向建图指的是把边的指向反转,和边权无关,想一想什么时候用...

Crossing over 254

hdu 1535 Invitation Cards

点击打开链接hdu1535 思路:最短路+SPFA 分析: 1 题目要求的是总的最小的花费,意思就是要求每一个人的花费都最小。 2 由于每一个人都是从1出去,最后还是都要回到1的,那么求解的时候就要分成两部分“出去+回来”;出去的话直接利用SPFA(1),1作为起点即可求出每一点的最小花费,回来的话如果是直接利用对每一个人进行SPFA,那么这样肯定超时。仔细想想要求的是每一个点到1的最...

我是传奇 256

hdu1535Invitation Cards(spfa)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1535 Invitation Cards Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 4143    Accepted S

zhiyuanjiang的博客 633

HDU1535Invitation Cards

Invitation Cards Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 3641    Accepted Submission(s): 1704 Problem Description In the age o

wo的只属于我 397

【SPFA邻接表】HDU 1535 Invitation Cards

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1535

F丶轩 580

Invitation Cards HDU - 1535 反向建图

一、内容 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....

Tizzi的博客 1212

hdu 1535 Invitation Cards(SPFA)

hdu 1535 Invitation Cards 题意:就是一个人需要走很多

程序员的冷浪漫 911

Invitation Cards HDU - 1535

我们需要为每个志愿者计算从中央检查站(CCS,编号为1)到其分配站点的往返最小交通费用之和。所有公交线路是单向的,要求找到所有志愿者总费用的最小值。问题的核心是:计算所有站点到CCS的最短路径和CCS到所有站点的最短路径之和。- 反向图:将原图的边反向,计算CCS到所有站点的最短路径(等效于原图中所有站点到CCS的最短路径)该方案通过双向最短路径计算,高效解决了大规模交通网络中的费用最小化问题,代码经过优化可处理百万级节点和边。- 正向图:计算CCS到所有站点的最短路径(Dijkstra算法)

2301_80020275的博客 1011

HDU1535 Invitation Cards(dijkstra)

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, Ant...

邵光亮的博客 14万+

HDU1535 Invitation Cards 最短路

传送门:【HDU1535 Invitation Cards

poursoul 700

HDU - 1535 Invitation Cards

题目: 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. T...

qq_41998938的博客 191
上一篇: POJ2663——Tri Tiling
下一篇: POJ1655——Balancing Act
tokers
博客等级 码龄12年 71粉丝 700原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值