ZOJ-3946 Highway Project (最短路)

题目大意:一张带权无向图,权有两个参数(d,c),分别表示走过这条边的时间和建造这条边的代价。要求选出一些边,使得0节点到其他点的距离之和最短,并在最短的基础上求最小代价。

题目分析:这是16年浙江省赛的一道题。先求出0到所有点的最短路,然后找出所有可能在最短路径上的边,最后在每一个节点的入边之中都选一条具有最小代价的边。多么简单的一道题!!!

代码如下:

# include<iostream>
# include<cstdio>
# include<cstring>
# include<vector>
# include<queue>
# include<list>
# include<set>
# include<map>
# include<string>
# include<cmath>
# include<cstdlib>
# include<algorithm>
using namespace std;
# define LL long long

const int N=1005;
const int INF=1000000000;
const LL oo=1000000000000005;

struct Edge
{
	int to,nxt;
	LL c,d;
};

int n,m,cnt;
Edge e[N*200];
LL d[N*100];
int head[N*100];
vector<int>pre[N*100];

void add(int u,int v,LL d,LL c)
{
	e[cnt].to=v;
	e[cnt].d=d;
	e[cnt].c=c;
	e[cnt].nxt=head[u];
	head[u]=cnt++;
}

void solve()
{
	for(int i=0;i<n;++i)
		pre[i].clear();

	fill(d,d+n,oo);
	d[0]=0;
	queue<int>q;
	q.push(0);
	while(!q.empty())
	{
		int u=q.front();
		q.pop();
		for(int i=head[u];i!=-1;i=e[i].nxt){
			int v=e[i].to;
			if(d[v]>d[u]+e[i].d){
				d[v]=d[u]+e[i].d;
				q.push(v);
			}
		}
	}

	q.push(0);
	while(!q.empty()){
		int u=q.front();
		q.pop();
		for(int i=head[u];i!=-1;i=e[i].nxt){
			int v=e[i].to;
			if(d[v]==d[u]+e[i].d){
				pre[v].push_back(i);
				q.push(v);
			}
		}
	}

	LL ans1=0,ans2=0;
	for(int i=0;i<n;++i){
		ans1+=d[i];
		LL minn=oo;
		for(int j=0;j<pre[i].size();++j)
			minn=min(minn,e[pre[i][j]].c);
		if(minn==oo) continue;
		ans2+=minn;
	}
	printf("%lld %lld\n",ans1,ans2);
}

int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d",&n,&m);
		cnt=0;
		memset(head,-1,sizeof(head));
		int a,b;
		LL c,d;
		for(int i=0;i<m;++i){
			scanf("%d%d%lld%lld",&a,&b,&d,&c);
			add(a,b,d,c);
			add(b,a,d,c);
		}
		solve();
	}
	return 0;
}

  

时间: 2024-08-05 23:19:33

ZOJ-3946 Highway Project (最短路)的相关文章

ZOJ 3946 Highway Project 贪心+最短路

题目链接: http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=3946 题解: 用dijkstra跑单元最短路径,如果对于顶点v,存在一系列边(ui,v)使得dis[v]最小(dis[v]表示0到v的距离).这些边能且只能选一条,那么我们自然应该选cost最小的那个边了. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #inc

zoj 3946 Highway Project spfa

题意:一个帝国有 n 个城市,可以在城市两两之间建立 m 条高速公路,建立 x-y 之间的高速路需要时间 d,花费为 c, 最后建立完边(<=m)后使得首都 0 号城市到各个城市(1~n-1)的总时间最少,在多个时间满足条件下再选花费最少的. 思路:直接spfa,当有多个点使得时间最少时,选花费最小的边. #include <iostream> #include <algorithm> #include <string.h> #include <stdio.

ZOJ 3946 Highway Project

迪杰斯特拉最小堆 #include<cstdio> #include<cstring> #include<cmath> #include<map> #include<queue> #include<algorithm> using namespace std; const long long INF=9999999999999; const int maxn=2e5+10; struct X{ int id; long long ti

ZOJ - 3794 Greedy Driver 最短路

首先正向跑一遍1为起点的最短路,注意松弛过程如果走到加油站则dis=0,并且路上任意时刻dis都不能大于C,判断dis[n]是否<=C就能判断无解情况了. 然后反向建图再跑一次N为起点的最短路,这样可以求到每个点到n点的最短路. 对于每一个可以交易的城市,C-dis1[i]-dis2[i]就是多出来可以卖掉的油. #include<iostream> #include<cstdlib> #include<cstring> #include<cmath>

zoj 1655 单源最短路 改为比例+最长路

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=655 没有理解清题意就硬套模板,所以WA了好几次, 解析看我的另一篇http://blog.csdn.net/u011026968/article/details/35579035 贴代码 #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #de

Zoj 1655 Transport Goods 最短路的应用

这道题目真是充分显示了窝的智商低下,首先题目在有中文翻译的情况下看了半天没看懂,= = 然后已知这题的分类是最短路了还是不会做,= = 一开始想着在dij扩展的的时候就把最大运送值算好,不过后来发现正向运输和反向运输的值显然是不相等的o(╯□╰)o 后来直接把每个城市作为起点dij了,反正n就一百,n^3就n^3了,后来发现可以从多条路一起送到终点 无奈看了题解,发现原来dij算的应该是终点到各个点的最小费率(最大比率),这样不仅正向反向算结果是一样的,而且不会漏. 话说题目本来就是要把每个城市

zoj 2027 Travelling Fee (最短路变形)

Travelling Fee Time Limit: 2 Seconds      Memory Limit: 65536 KB Samball is going to travel in the coming vacation. Now it's time to make a plan. After choosing the destination city, the next step is to determine the travel route. As this poor guy ha

zoj 1221 Risk【最短路 floyd】

ZOJ Problem Set - 1221 Risk Time Limit: 2 Seconds      Memory Limit: 65536 KB Risk is a board game in which several opposing players attempt to conquer the world. The gameboard consists of a world map broken up into hypothetical countries. During a p

zoj3946--Highway Project

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 highw