ZOJ - 3946 (Dijkstra的变形)

ZOJ - 3946(Dijkstra的变形)

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 ≤ i ≤ N). 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


题目大意:

一个国家有n个城市,标号为0-n-1 首都是0,现在国王要修路,有m条路可以修,每条路都有一个价值花费,就是修这条路需要多少钱,还有一个时间花费,就是走这条路所需要的时间。
要求是:从首都到其他的城市花费的总时间最小(0-1 0-2 0-3 …0-n-1 这些时间和加起来),在这个前提下要求花的钱最少。

解题思路:

就是跑一个单源最短路,让每一个点到原点0 的“距离”最小,这个距离是抽象的距离,先以时间为第一标准,以花费为第二标准。
开两个数组,dis_time[i] 表示 从原点到i的最短时间 dis_cost[i]:到i这个点的最小花费的边,只是一条,而不是和dis_time一样是从原点到i,这个地方想明白了,更新的时候就很好更了

for(int i=head[now.to];i!=-1;i=side[i].next){
			int v=side[i].to;
			if(dis_time[v]>dis_time[now.to]+side[i].time){
				dis_time[v]=dis_time[now.to]+side[i].time;
				dis_cost[v]=side[i].cost;
				q.push(Node(v,dis_time[v],dis_cost[v]));
			}
			else if(dis_time[v]==dis_time[now.to]+side[i].time){
				if(dis_cost[v]>side[i].cost){
					dis_cost[v]=side[i].cost;
					dis_time[v]=dis_time[now.to]+side[i].time;
					q.push(Node(v,dis_time[v],dis_cost[v]));
				}
			}
	}

先以时间为标准,再以花费为标准。
想明白怎么更新,剩下的就是裸的Dijkstra了。
然后最后把 两个dis数组里面的内容加起来就是所求的解了。

AC代码:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1e5+10;
int head[maxn];
int cnt;
ll dis_time[maxn];
int dis_cost[maxn];
int vis[maxn];
int n,m;
int cost=0;
struct node{
	int to;
	int time;
	int cost;
	int next;
}side[maxn*2];
struct Node{
	int time;
	int cost;
	int to;
	Node(){}
	Node(int T,int time,int cost):to(T),time(time),cost(cost){}
	friend bool operator <(struct Node x,struct Node y){
		if(x.time!=y.time)
			return x.time>y.time;
		else 
			return x.cost>y.cost;
	}
};
void init(){
	memset(head,-1,sizeof(head));
	memset(dis_cost,0x3f3f3f3f,sizeof(dis_cost));
	memset(dis_time,0x7f7f7f7f,sizeof(dis_time));
	memset(vis,0,sizeof(vis));
	cnt=0;
}
void add(int x,int y,int t,int c){
	side[cnt].to=y;
	side[cnt].time=t;
	side[cnt].cost=c;
	side[cnt].next=head[x];
	head[x]=cnt++;
}
void Dijkstra(){
	dis_cost[0]=0;
	dis_time[0]=0;
	priority_queue<Node> q;
	q.push(Node(0,0,0));
	while(q.size()){
		Node now=q.top();
		q.pop();
		//cout<<now.to<<endl;
		if(vis[now.to]) continue;
		vis[now.to]=1;
		for(int i=head[now.to];i!=-1;i=side[i].next){
			int v=side[i].to;
			if(dis_time[v]>dis_time[now.to]+side[i].time){
				dis_time[v]=dis_time[now.to]+side[i].time;
				dis_cost[v]=side[i].cost;
				q.push(Node(v,dis_time[v],dis_cost[v]));
			}
			else if(dis_time[v]==dis_time[now.to]+side[i].time){
				if(dis_cost[v]>side[i].cost){
					dis_cost[v]=side[i].cost;
					dis_time[v]=dis_time[now.to]+side[i].time;
					q.push(Node(v,dis_time[v],dis_cost[v]));
				}
			}
		}
	}

}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--){
		scanf("%d%d",&n,&m);
		init();
		while(m--){
			int u,v,t,c;
			scanf("%d%d%d%d",&u,&v,&t,&c);
			add(u,v,t,c);
			add(v,u,t,c);
		}
		Dijkstra();
		ll ans1=0,ans2=0;
		for(int i=0;i<n;i++){
			ans1+=dis_time[i];
			ans2+=(ll)dis_cost[i];
		}
		cout<<ans1<<" "<<ans2<<endl;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值