Highway Project---zoj3946(最短路SPFA)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5718
题意:
有n个点 m(n,m<=10^5)条路,现在要建路,每条路连接uv两个点,所需时间是time花费是cost,现要求从0点到达其他点的,让时间和最小,当有多种选择时要求修路的总花费最小,输出最小时间及花费;
可以看成最短路问题
我们定义两个数组Time[i]表示从起点到 i 点所需的最小时间,Cost[i]表示到达 i 的那个点 u 到 i 的最小花费;其实就相当于是最短路中的dist和最小生成树中的dist
需要注意的是,本题数据超int了,所以初始化的时候要注意最大值的值;
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
#define N 100050
typedef long long LL;
const LL INF = (1ll<<)-;
struct node
{
int v;
LL cost, time;
node(int v0=, LL c=, LL t=) : v(v0),cost(c), time(t){}
}; vector<vector<node> >G;
int n, vis[N];
LL Cost[N], Time[N]; void SPFA()
{
for(int i=; i<n; i++)
{
Cost[i] = Time[i] = INF;
vis[i] = ;
}
Cost[] = Time[] = ; queue<int>Q;
Q.push(); while(Q.size())
{
int p = Q.front(); Q.pop(); vis[p] = ; int len = G[p].size();
for(int i=; i<len; i++)
{
node q = G[p][i];
if(Time[q.v] > Time[p]+q.time || (Time[q.v]==Time[p]+q.time&&Cost[q.v]>q.cost))
{
Time[q.v] = Time[p]+q.time;
Cost[q.v] = q.cost;
if(!vis[q.v])
{
vis[q.v] = ;
Q.push(q.v);
}
}
}
}
LL TimeSum = , CostSum = ;
for(int i=; i<n; i++)
{
TimeSum += Time[i];
CostSum += Cost[i];
}
printf("%lld %lld\n", TimeSum, CostSum);
} int main()
{
int T, m;
//cout<<INF;
scanf("%d", &T);
while(T--)
{
scanf("%d %d", &n, &m);
G.clear();
G.resize(n+); for(int i=; i<m; i++)
{
int u, v;
LL cost, time;
scanf("%d %d %lld %lld", &u, &v, &time, &cost);
G[u].push_back(node(v, cost, time));
G[v].push_back(node(u, cost, time));
} SPFA();
}
return ;
}
Highway Project---zoj3946(最短路SPFA)的更多相关文章
- ZOJ3946:Highway Project(最短路变形)
本文转载自:http://www.javaxxz.com/thread-359442-1-1.html Edward, the emperor of the Marjar Empire, wants ...
- ZOJ-3946 Highway Project (最短路)
题目大意:一张带权无向图,权有两个参数(d,c),分别表示走过这条边的时间和建造这条边的代价.要求选出一些边,使得0节点到其他点的距离之和最短,并在最短的基础上求最小代价. 题目分析:这是16年浙江省 ...
- ZOJ 3946 Highway Project 贪心+最短路
题目链接: http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=3946 题解: 用dijkstra跑单元最短路径,如果对于顶点v,存 ...
- (spfa) Highway Project (zoj 3946 )
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5718 Highway Project Time Limit: 2 Seco ...
- ZOJ 3946.Highway Project(The 13th Zhejiang Provincial Collegiate Programming Contest.K) SPFA
ZOJ Problem Set - 3946 Highway Project Time Limit: 2 Seconds Memory Limit: 65536 KB Edward, the ...
- zoj 3946 Highway Project(最短路 + 优先队列)
Highway Project Time Limit: 2 Seconds Memory Limit: 65536 KB Edward, the emperor of the Marjar ...
- 最短路模板(Dijkstra & Dijkstra算法+堆优化 & bellman_ford & 单源最短路SPFA)
关于几个的区别和联系:http://www.cnblogs.com/zswbky/p/5432353.html d.每组的第一行是三个整数T,S和D,表示有T条路,和草儿家相邻的城市的有S个(草儿家到 ...
- ZOJ 3946 Highway Project(Dijkstra)
Highway Project Time Limit: 2 Seconds Memory Limit: 65536 KB Edward, the emperor of the Marjar ...
- L - Subway(最短路spfa)
L - Subway(最短路spfa) You have just moved from a quiet Waterloo neighbourhood to a big, noisy city. In ...
随机推荐
- CentOS命令top下你不一定懂的cpu显示信息
在使用top命令的时候会看到这么一行: 里面的各个值分别是什么意思呢? 今天被问到这个问题,发现答的不是很清楚.果然啊,天天用最多的top命令都还没摸透...惭愧...于是就查了些资料: 官方解释 C ...
- cake build使用:
开源地址: https://github.com/cake-build/cake 依赖 powershell 3.0 Windows 获取引导程序: Invoke-WebRequest http:// ...
- EF修改对象里面的值。。。(对象字段多的时候)
后台代码 public ActionResult Edit(my m)//my实体类 { testEntities t = new testEntities();//数据库上下文 t.my.Attac ...
- MVC生成图片验证码,可指定位数
前台: <h2>mvc后台生成验证码,可指定位数</h2> <img id="gc" src="GetValidateCode" ...
- jquery获取当前select下拉选的属性值
body中: <li> <select id="select_phone"></select> <input type="but ...
- ubuntu网络配置命令
Ubuntu网络配置例如: (1) 配置eth0的IP地址, 同时激活该设备. #ifconfig eth0 192.168.1.10 netmask 255.255.255.0 up (2) 配置e ...
- java socket通信-传输文件图片--传输图片
ClientTcpSend.java client发送类 package com.yjf.test; import java.io.DataOutputStream; import java.io ...
- VS------csc.exe已停止工作解决方法
转载: http://blog.csdn.net/verylost/article/details/53667769 方法: 解决方法是把进程中所有的VBCSCompiler.exe进程结束掉,然后清 ...
- Java类的设计----方法的重写、覆盖
方法的重写.覆盖 在子类中可以根据需要对从父类中继承来的方法进行改造—覆盖方法(方法的重置.重写),在程序执行时,子类的方法将覆盖父类的方法. 覆盖方法必须和被覆盖方法具有相同的方法名称.参数列表和返 ...
- Effective C++ Item 33 Avoid hiding inherited names
class Base { private: int x; public: ; virtual void mf2(); void mf3(); ... }; class Derived: public ...