ZOJ 3946 Highway Project
1.迪杰斯特拉最小堆
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<queue>
#include<algorithm>
using namespace std; const long long INF=;
const int maxn=2e5+;
struct X{
int id;
long long time,cost;
bool operator < (const X &a) const
{
if(time==a.time) return cost>a.cost;
return time>a.time;
}
X(int f,long long g,long long h)
{
id=f,time=g,cost=h;
}
}; struct Edge
{
int from,to;
long long time,cost;
int next;
}s[maxn];
int head[maxn];
bool flag[maxn];
long long ans1,ans2;
long long dis[maxn]; int main()
{
int T,n,m; scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m); memset(head,-,sizeof head);
memset(flag,,sizeof flag);
ans1=ans2=;
for(int i=;i<=n;i++) dis[i]=INF; int cnt=;
for(int i=;i<=m;i++)
{
int u,v;
long long time,cost;
scanf("%d%d%lld%lld",&u,&v,&time,&cost); s[cnt].from=u,s[cnt].to=v,s[cnt].time=time,s[cnt].cost=cost;
s[cnt].next=head[u],head[u]=cnt++; s[cnt].from=v,s[cnt].to=u,s[cnt].time=time,s[cnt].cost=cost;
s[cnt].next=head[v],head[v]=cnt++;
} priority_queue<X>q;
q.push(X(,,)); dis[]=; while(!q.empty())
{
X h=q.top(); q.pop();
if(flag[h.id]==) continue; flag[h.id]=; ans1=ans1+h.time; ans2=ans2+h.cost; for(int i=head[h.id];i!=-;i=s[i].next)
{
if(flag[s[i].to]==&&dis[s[i].to]>=dis[h.id]+s[i].time)
{
dis[s[i].to]=dis[h.id]+s[i].time;
q.push(X(s[i].to,dis[h.id]+s[i].time,s[i].cost));
}
}
}
printf("%lld %lld\n",ans1,ans2);
}
return ;
}
2.先处理出哪些边是最短路上的,这样就构成了一个新的有向无环图,注意是无环。
每一个点最后肯定是连通的,那么我们只需要选择连到这个点的费用最小的边即可。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
using namespace std; int T,n,m;
long long INF=;
const int maxn=+;
struct Edge
{
int from,to,next;
long long t,c;
}e[*maxn];
int tot,head[maxn];
long long dis[maxn],cost[maxn];
bool flag[maxn]; void add(int a,int b,int t,int c)
{
e[tot].from=a, e[tot].to=b;
e[tot].t=t, e[tot].c=c;
e[tot].next=head[a];
head[a]=tot++;
} void SPFA()
{
for(int i=;i<=n;i++) dis[i]=INF;
memset(flag,,sizeof flag);
queue<int>Q;
dis[]=; Q.push(); flag[]=;
while(!Q.empty())
{
int h=Q.front(); Q.pop(); flag[h]=;
for(int i=head[h];i!=-;i=e[i].next)
{
if(dis[h]+e[i].t<dis[e[i].to])
{
dis[e[i].to]=dis[h]+e[i].t;
if(flag[e[i].to]==)
{
flag[e[i].to]=;
Q.push(e[i].to);
}
}
}
}
} int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
memset(head,-,sizeof head); tot=;
for(int i=;i<=m;i++)
{
int u,v;long long t,c; scanf("%d%d%lld%lld",&u,&v,&t,&c);
add(u,v,t,c); add(v,u,t,c);
}
SPFA();
long long ans1=,ans2=;
for(int i=;i<n;i++) ans1=ans1+dis[i];
for(int i=;i<=n;i++) cost[i]=INF;
for(int i=;i<tot;i++)
if(dis[e[i].from]+e[i].t==dis[e[i].to])
cost[e[i].to]=min(cost[e[i].to],e[i].c);
for(int i=;i<n;i++) ans2=ans2+cost[i];
printf("%lld %lld\n",ans1,ans2);
}
return ;
}
ZOJ 3946 Highway Project的更多相关文章
- 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 ...
- ZOJ 3946 Highway Project(Dijkstra)
Highway Project Time Limit: 2 Seconds Memory Limit: 65536 KB Edward, the emperor of the Marjar ...
- ZOJ 3946 Highway Project 贪心+最短路
题目链接: http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=3946 题解: 用dijkstra跑单元最短路径,如果对于顶点v,存 ...
- ZOJ 3946 Highway Project (最短路)
题意:单源最短路,给你一些路,给你这些路的长度,给你修这些路的话费,求最短路和最小花费. 析:本质就是一个最短路,不过要维护两个值罢了,在维护花费时要维护的是该路要花多少,而不是总的路线花费. 代码如 ...
- (spfa) Highway Project (zoj 3946 )
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5718 Highway Project Time Limit: 2 Seco ...
- 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年浙江省 ...
- The 13th Zhejiang Provincial Collegiate Contest(2016年浙江省赛)
前4道水题就不说了,其中我做了C题,1Y,小心仔细写代码并且提交之前得确认无误后提交才能减少出错率. 结果后面2题都由波神做掉,学长带我们飞~ 终榜 官方题解 ZOJ 3946 Highway ...
随机推荐
- zookeeper集群实例
zookeeper是什么 Zookeeper,一种分布式应用的协作服务,是Google的Chubby一个开源的实现,是Hadoop的分布式协调服务,它包含一个简单的原语集,应用于分布式应用的协作服务, ...
- redis学习一
一.简介: 在过去的几年中,NoSQL数据库一度成为高并发.海量数据存储解决方案的代名词,与之相应的产品也呈现出雨后春笋般的生机.然而在众多产品中能够脱颖而出的却屈指可数,如Redis.MongoDB ...
- 转 Oracle DBCA高级玩法:从模板选择、脚本调用到多租户
但凡是学过Oracle的同学,对DBCA(Database Configuration Assistant, DBCA)都不会陌生,有了这个工具,使得创建数据库成为可能.而DBCA本身有图形和静默两种 ...
- [转]理解SSL(https)中的对称加密与非对称加密
加密 解密 Tweet 密码学最早可以追溯到古希腊罗马时代,那时的加密方法很简单:替换字母. 早期的密码学 古希腊人用一种叫 Scytale 的工具加密.更快的工具是 transposition ...
- 当使用System,out.println()打印一个对象是自动调用toString方法
在Java中,所有的对象都是继承自Object,自然继承了toString方法,在当使用System,out.println()里面为一个对象的引用时,自动调用toString方法讲对象打印出来.如果 ...
- android touchEvent事件学习
学习网址:http://www.apkbus.com/forum.php?mod=viewthread&tid=44296 1:Android Touch事件传递机制解析 android系统中 ...
- android 数据存储分配的一些事
应用程序在运行的过程中如果需要向手机上保存数据,一般是把数据保存在SDcard中的.大部分应用是直接在SDCard的根目录下创建一个文件夹,然后把数据保存在该文件夹中.这样当该应用被卸载后,这些数据还 ...
- Hibernate锁机制
业务逻辑的实现过程中,往往需要保证数据访问的排他性.因此,我们就需要通过一些机制来保证这些数据在某个操作过程中不会被外界修改,这样的机制,在这里,也就是所谓的“锁”,即给我们选定的目标数据上锁,使其无 ...
- panel 绑定鼠标滚轮事件
void formsample_mousewheel(object sender, MouseEventArgs e) { //获取光标位置 Point mousepoint = new Point( ...
- MFC的核心概念
API是英文Application Programming Interface 的缩写,意思是“应用程序接口”,泛指系统为应用程序提供的一系列函数接口,在编程时可以直接调用,而不必知道其内部实现的过程 ...