Aizu2249(spfa || djstr+堆优化)两个参数的最短路

题目链接:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2249

 Road Construction

King Mercer is the king of ACM kingdom. There are one capital and some cities in his kingdom. Amazingly, there are no roads in the kingdom now. Recently, he planned to construct roads between the capital and the cities, but it turned out that the construction cost of his plan is much higher than expected.

In order to reduce the cost, he has decided to create a new construction plan by removing some roads from the original plan. However, he believes that a new plan should satisfy the following conditions:

  • For every pair of cities, there is a route (a set of roads) connecting them.
  • The minimum distance between the capital and each city does not change from his original plan.

Many plans may meet the conditions above, but King Mercer wants to know the plan with minimum cost. Your task is to write a program which reads his original plan and calculates the cost of a new plan with the minimum cost.

Input

The input consists of several datasets. Each dataset is formatted as follows.

N M
uvdc
.
.
.
uM vM dM cM 

The first line of each dataset begins with two integers, N and M (1 ≤ N ≤ 10000, 0 ≤ M ≤ 20000). N and Mindicate the number of cities and the number of roads in the original plan, respectively.

The following M lines describe the road information in the original plan. The i-th line contains four integers, uividi and ci (1 ≤ uivi ≤ N , ui ≠ vi , 1 ≤ di ≤ 1000, 1 ≤ ci ≤ 1000). ui , vidi and ci indicate that there is a road which connects ui-th city and vi-th city, whose length is di and whose cost needed for construction is ci.

Each road is bidirectional. No two roads connect the same pair of cities. The 1-st city is the capital in the kingdom.

The end of the input is indicated by a line containing two zeros separated by a space. You should not process the line as a dataset.

Output

For each dataset, print the minimum cost of a plan which satisfies the conditions in a line.

Sample Input

3 3
1 2 1 2
2 3 2 1
3 1 3 2
5 5
1 2 2 2
2 3 1 1
1 4 1 1
4 5 1 1
5 3 1 1
5 10
1 2 32 10
1 3 43 43
1 4 12 52
1 5 84 23
2 3 58 42
2 4 86 99
2 5 57 83
3 4 11 32
3 5 75 21
4 5 23 43
5 10
1 2 1 53
1 3 1 65
1 4 1 24
1 5 1 76
2 3 1 19
2 4 1 46
2 5 1 25
3 4 1 13
3 5 1 65
4 5 1 34
0 0

Output for the Sample Input

3
5
137
218

Source: ACM International Collegiate Programming Contest , ACM-ICPC Japan Alumni Group Practice Contest 2010, 2010-11-28 
http://acm-icpc.aitea.net/

题目大意:给一个无向图,m条边,每个边有两个quan权值,长度和花费,在选最短的长度的时候选zui'最少花费的那条路,然后是一个最短路,zha找出最小hua花费的总和,然后输出zui'最小花费的总和。

这个题有两种方法,djstr优化一下可以,比较简单,就先贴出来了:

注意输入结束时是零。。我一直时&&。。wa了一天半。。心态炸裂!!

#include<stdio.h>
#include<string.h>  
#include<math.h>  
  
//#include<map>   
//#include<set>
#include<deque>  
#include<queue>  
#include<stack>  
#include<bitset> 
#include<string>  
#include<iostream>  
#include<algorithm>  
using namespace std;  
  
#define ll long long  
#define INF 0x3f3f3f3f  
#define mod 1000000
#define clean(a,b) memset(a,b,sizeof(a))// 水印 

struct node{
	int ed,l,cost;
	node(int _ed=0,int _l=0,int _cost=0):ed(_ed),l(_l),cost(_cost){}
//	node(int _v=0,int _c=0){
//		v=_v;
//		c=_c;
//	}
//	bool operator < (const node &a)const{
//		return
//	}
};
struct cmp{
	bool operator ()(const node &a,const node &b){
		if(a.l==b.l)
			return a.cost>b.cost;
		return a.l>b.l;
	}
};
struct Edge{//邻接表 
	int ed,l,cost;
	Edge(int _ed=0,int _l=0,int _cost=0):ed(_ed),l(_l),cost(_cost){}
};

vector<Edge>E[100100];
bool vis[100100];
int dist[100100],distv[100100];

void djstr(int n,int start)
{
	clean(vis,0);
	clean(dist,INF);
	clean(distv,INF);
	priority_queue<node,vector<node>,cmp> que;//优先队列node 
	while(!que.empty())
		que.pop();
	dist[start]=0;
	distv[start]=0;
	que.push(node(start,0,0));
	
//	for(int i=0;i<=n;++i)
//	{
//		for(int j=0;j<E[i].size();++j)
//			cout<<E[i][j].v<<" "<<E[i][j].cost<<endl;
//	}
	
	while(!que.empty())
	{
		node now=que.top();
		que.pop();
		int u=now.ed;
		if(vis[u])
			continue;
		vis[u]=1;
		//cout<<u<<" ";
		for(int i=0;i<E[u].size();++i)
		{
			int v=E[u][i].ed;
			int l=E[u][i].l;
			int cost=E[u][i].cost;
			//cout<<v<<" "<<cost<<endl;
			if(!vis[v]&&dist[v]>dist[u]+l)
			{
				dist[v]=dist[u]+l;
				distv[v]=cost;
				que.push(node(v,dist[v],cost));
			}
			else if(!vis[v]&&dist[v]==dist[u]+l&&distv[v]>cost)
				distv[v]=cost;
//			for(int i=1;i<=n;++i)
//				cout<<distv[i]<<" ";
//			cout<<endl;
		}
	}
}

void add(int op,int ed,int l,int c)
{
	E[op].push_back(Edge(ed,l,c));
}

int main()
{
	int n,m;
	while(cin>>n>>m&&n!=0||m!=0)
	{
		for(int i=0;i<100100;++i)
			E[i].clear();
		for(int i=1;i<=m;++i) 
		{
			int op,ed,l,cost;
			cin>>op>>ed>>l>>cost;
			add(op,ed,l,cost);
			add(ed,op,l,cost);
		}
		djstr(n,1);
//		for(int i=1;i<=n;++i)
//			cout<<distv[i]<<" ";
//		cout<<endl;
		int sum=0;
		for(int i=1;i<=n;++i)
			sum=sum+distv[i];
		cout<<sum<<endl;
	}
}

然后是spfa,有点像djstr,(个人感觉就是优化的djstr)

:用到了一个叫做链式向前星的数组模拟邻接表,其实也没什么,可以判断负环

#include<stdio.h>
#include<string.h>  
#include<math.h>  
  
//#include<map>   
//#include<set>
#include<deque>  
#include<queue>  
#include<stack>  
#include<bitset> 
#include<string>  
#include<iostream>  
#include<algorithm>  
using namespace std;  
  
#define ll long long  
#define INF 0x3f3f3f3f  
#define mod 1000000
#define clean(a,b) memset(a,b,sizeof(a))// 水印 
//spfa
//https://cn.vjudge.net/contest/239171#problem/C 
struct node{
    int to,next,dis,cost;
}edge[40100];
int head[10100];
int cost[10100],dis[10100];
bool vst[10100];
int n,m,k;

void spfa(int x)
{
	clean(dis,INF);
	clean(vst,0);
	clean(cost,INF);
    queue<int> q;
    dis[x]=0;
    cost[x]=0;
    q.push(x);
    while(!q.empty())
	{
        x=q.front();q.pop();
        vst[x]=0;
        for(int i=head[x];i!=-1;i=edge[i].next)
		{
            node b=edge[i];
            if(dis[b.to]>dis[x]+b.dis)
			{
                if(!vst[b.to])
				{
                    vst[b.to]=1;
                    q.push(b.to);
                }
                dis[b.to]=dis[x]+b.dis;
                cost[b.to]=b.cost;
            }
			else if(dis[b.to]==dis[x]+b.dis)
			{
                if(cost[b.to]>b.cost)
                    cost[b.to]=b.cost;
            }
        }
    }
}
void add(int a,int b,int dis,int cost)
{
    edge[k].to=b;
    edge[k].dis=dis;
    edge[k].cost=cost;
    edge[k].next=head[a];
    head[a]=k;
    k++;
}
int main()
{
    while(cin>>n>>m&&n!=0||m!=0)
	{
        clean(head,-1);
        k=0;
        for(int i=0;i<m;i++)
		{
            int a,b,d,c;
            cin>>a>>b>>d>>c;
            add(a,b,d,c);
            add(b,a,d,c);
        }
        spfa(1);
        ll re=0;
        for(int i=1;i<=n;i++){
            re+=cost[i];
        }
        cout<<re<<endl;
    }

    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值