【ZOJ3946 The 13th Zhejiang Provincial Collegiate Programming ContestK】【最短路+贪心 or 最小树形图】Highway Proje

Highway Project

Time Limit: 2 Seconds      Memory Limit: 65536 KB

Edward, the emperor of the Marjar Empire, wants to build some bidirectional highways so that he can reach other cities from the capital as fast as possible. Thus, he proposed the highway project.

The Marjar Empire has N cities (including the capital), indexed from 0 to N - 1 (the capital is 0) and there are M highways can be built. Building the i-th highway costs Ci dollars. It takes Di minutes to travel between city Xi and Yi on the i-th highway.

Edward wants to find a construction plan with minimal total time needed to reach other cities from the capital, i.e. the sum of minimal time needed to travel from the capital to city i (1 ≤ iN). Among all feasible plans, Edward wants to select the plan with minimal cost. Please help him to finish this task.

Input

There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

The first contains two integers N, M (1 ≤ N, M ≤ 105).

Then followed by M lines, each line contains four integers Xi, Yi, Di, Ci (0 ≤ Xi, Yi < N, 0 < Di, Ci < 105).

Output

For each test case, output two integers indicating the minimal total time and the minimal cost for the highway project when the total time is minimized.

Sample Input
2
4 5
0 3 1 1
0 1 1 1
0 2 10 10
2 1 1 1
2 3 1 2
4 5
0 3 1 1
0 1 1 1
0 2 10 10
2 1 2 1
2 3 1 2
Sample Output
4 3
4 4

Author: Lu, Yi
Source: The 13th Zhejiang Provincial Collegiate Programming Contest

#include<stdio.h> 
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 1e5+10, M = 2e5+10, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;
int casenum, casei;
int n, m;
struct node
{
	int x; LL dis;
	node(int x, LL dis) :x(x), dis(dis) {};
	bool operator < (const node&b)const
	{
		return dis > b.dis;
	}
};
int first[N], id;
int w[M], dis[M], cost[M], nxt[M];
void ins(int x, int y, int d, int c)
{
	w[++id] = y;
	dis[id] = d;
	cost[id] = c;
	nxt[id] = first[x];
	first[x] = id;
}
bool e[N];
LL f[N]; int pre[N];
priority_queue<node>q;
void inq(int x, LL d, int p)
{
	if (d > f[x])return;
	if (d == f[x])
	{
		gmin(pre[x], p);
		return;
	}
	f[x] = d;
	pre[x] = p;
	q.push(node(x, d));
}
void dijkstra()
{
	memset(f, 63, n << 3);
	memset(pre, 63, n << 2);
	memset(e, 0, n);
	inq(0, 0, 0);
	while (!q.empty())
	{
		int x = q.top().x; q.pop();
		if (e[x])continue;
		else e[x] = 1;
		for (int z = first[x]; z; z = nxt[z])inq(w[z], f[x] + dis[z], cost[z]);
	}
}
int main()
{
	scanf("%d", &casenum);
	for (casei = 1; casei <= casenum; ++casei)
	{
		scanf("%d%d", &n, &m);
		memset(first, 0, n << 2); id = 1;
		for (int i = 1; i <= m; ++i)
		{
			int x, y, d, c;
			scanf("%d%d%d%d", &x, &y, &d, &c);
			ins(x, y, d, c);
			ins(y, x, d, c);
		}
		dijkstra();
		LL totdis = 0;
		LL totcost = 0;
		for (int i = 1; i < n; ++i)
		{
			totdis += f[i];
			totcost += pre[i];
		}
		printf("%lld %lld\n", totdis, totcost);
	}
	return 0;
}
/*
【trick&&吐槽】
贪心就是力量!

【题意】
有n(1e5)个点,m(1e5)条双向边。 0base
对于每条边,有(x,y,dis,cost)
我们希望使得——
1,选择(0~m)条边,在选择的这些边的连通性条件下,使得——
2,0号点到其他所有点的距离dis之和(即0号点到其他所有点的最短路之和)尽可能小,
3,在满足1,2的条件下,我们所选的边的费用成本cost之和尽可能小。

【类型】
最小树形图 or 贪心

【分析】
首先,我们所选的边,显然一定都是来源于最短路上的边
在这个条件下,如果我们以0号点为源点,以最短路上的边为待选边,做一个最小树形图,显然就能得到答案。

然而,不会nlogn的最小树形图算法,我们该怎么办?
最小生成树算法是错误的
我们只要使得每条边的前驱边是所有可选前驱边中边权最小的即可。

【时间复杂度&&优化】
O(nlogn)

*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Antique Comedians of Malidinesia would like to play a new discovered comedy of Aristofanes. Putting it on a stage should be a big surprise for the audience so all the preparations must be kept absolutely secret. The ACM director suspects one of his competitors of reading his correspondece. To prevent other companies from revealing his secret, he decided to use a substitution cipher in all the letters mentioning the new play. Substitution cipher is defined by a substitution table assigning each character of the substitution alphabet another character of the same alphabet. The assignment is a bijection (to each character exactly one character is assigned -- not neccessary different). The director is afraid of disclosing the substitution table and therefore he changes it frequently. After each change he chooses a few words from a dictionary by random, encrypts them and sends them together with an encrypted message. The plain (i.e. non-encrypted) words are sent by a secure channel, not by mail. The recipient of the message can then compare plain and encrypted words and create a new substitution table. Unfortunately, one of the ACM cipher specialists have found that this system is sometimes insecure. Some messages can be decrypted by the rival company even without knowing the plain words. The reason is that when the director chooses the words from the dictionary and encrypts them, he never changes their order (the words in the dictionary are lexicographically sorted). String a1a2 ... ap is lexicografically smaller than b1b2 ... bq if there exists an integer i, i <= p, i <= q, such that aj=bj for each j, 1 <= j < i and ai < bi. The director is interested in which of his messages could be read by the rival company. You are to write a program to determine that. Input Output Sample Input 2 5 6 cebdbac cac ecd dca aba bac cedab 4 4 cca cad aac bca bdac Sample Output abcde Message cannot be decrypted.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值