zoj 3946 Highway Project(优先队列优化dijkstra算法)

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 Xiand 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 NM (1 ≤ NM ≤ 105).

Then followed by M lines, each line contains four integers XiYiDiCi (0 ≤ XiYi < N, 0 < DiCi < 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
数量级均为100000,用邻接矩阵会超空间,用结构体存图可能会超时,所以用vector和priority_queue来优化dijkstra即可
#include <stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const long long INF=1e15;
struct node{
	int s;
	int e;
   	long long time;
	long long cost;
};
struct node1{
    long long time;
    long long   cost;
};
struct node2{
	int to;
	long long time;
	long long cost;
	friend bool operator <(node2 a,node2 b)
	{
		if(a.time!=b.time)
		{
			return a.time>b.time;
		}
		return a.cost<b.cost;
	}
};
priority_queue<node2>q;
vector<node>map;
vector<int>f[100005];
node1 minload[100005];
bool visit[100005]; 
int n,m;
void inint()
{
	int i;
	for(i=0;i<=n;i++)
	{
		map.clear();
		f[i].clear();
		minload[i].time=INF,minload[i].cost=INF;
		visit[i]=0;
	}
	while(!q.empty())
	{
		q.pop();
	}
}
int main(int argc, char *argv[])
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %d",&n,&m);
        int i,j;
        inint();
        int cnt=0;
        for(i=0;i<m;i++)
        {        
        	int s,e;
			long long time,cost; 
			node have;
            scanf("%d %d %lld %lld",&have.s,&have.e,&have.time,&have.cost);
			map.push_back(have); 
			f[map[map.size()-1].s].push_back(map.size()-1);
           
		    node have1;
			have1.e=have.s,have1.s=have.e,have1.time=have.time,have1.cost=have.cost;
			map.push_back(have1);
            f[map[map.size()-1].s].push_back(map.size()-1);
        }    
        minload[0].cost=0;
        minload[0].time=0;
        node2 go;
		go.to=0;
		go.time=0;
		go.cost=0;    
		q.push(go);
        while(!q.empty())
        {
            node2 next=q.top();
		    q.pop();
            if(visit[next.to])
            continue;	
            visit[next.to]=1;
            for(j=0;j<f[next.to].size();j++)
            {
            	 
                if(minload[map[f[next.to][j]].e].time>map[f[next.to][j]].time+minload[next.to].time)
                {
                    minload[map[f[next.to][j]].e].time=map[f[next.to][j]].time+minload[next.to].time;    
                    minload[map[f[next.to][j]].e].cost=map[f[next.to][j]].cost;
                    
                    node2 add;
                    add.to=map[f[next.to][j]].e;
                    add.time=minload[map[f[next.to][j]].e].time;
                    add.cost=minload[map[f[next.to][j]].e].cost;
                    q.push(add);
                }
               	else  if(minload[map[f[next.to][j]].e].time==map[f[next.to][j]].time+minload[next.to].time&&minload[map[f[next.to][j]].e].cost>map[f[next.to][j]].cost)
                {
                   	minload[map[f[next.to][j]].e].cost=map[f[next.to][j]].cost;
              	 	node2 add;
              	 	add.to=map[f[next.to][j]].e;
                    add.time=minload[map[f[next.to][j]].e].time;
                    add.cost=minload[map[f[next.to][j]].e].cost;
                    q.push(add);
                }
            }
        }
        long long max1time=0;
        long long max1cost=0;
        for(i=1;i<n;i++)
        {
            max1time+=minload[i].time;
            max1cost+=minload[i].cost;
        }
       printf("%lld %lld\n",max1time,max1cost);
    }    
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值