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 ...
随机推荐
- GCD is Funny
GCD is Funny Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Pro ...
- MyBaits 错误分析
错误原因:在DAO的映射文件中,在映射标签中的type类型写成DAO类了,应该写成javaBean
- gSoap客户端示例程序
先给出wsdl2h和soapcpp2的生成客户端代码 wsdl2h -s -o calc.h http://www.genivia.com/calc.wsdl soapcpp2 -i -C -x -I ...
- margin叠加相邻两个元素的上下margin是叠加在一起
<!doctype html><html><head><meta charset="utf-8"><title>无标题文 ...
- 【转载】GDI 映像方式 之 SetViewportOrgEx 与 SetWindowOrgEx 解析
SetViewportOrgEx 与 SetWindowOrgEx 解析 这两个函数,用来改变视端口和窗口的原点,并都具有改变轴的效果,以致(0,0)不再指左上角. 「视端口」是依据设备坐标(图素)的 ...
- struts2+ajax实现异步验证实现
由于老师布置作业的需要,在添加管理员的时候,要实现验证添加的管理员的用户名是否在数据库中已经存在,然后再客户端给用户一个提示.我首先想到的就是利用ajax实现异步验证技术,由于利用的ssh框架,所以在 ...
- Git学习 -- 标签管理
新建标签 git tag <tagname> 默认为HEAD,也可以指定一个commit id eg. git tag v0.9 git tag v1.0 31aa59c git ...
- ping命令使用技巧(一次Ping多个地址)
打开windows 命令行 窗口, 在命令行输入以下命令: for /l %i in (1,1,255) do ping -n 1 -w 60 192.168.0.%i | find " ...
- 使用Java管理千台规模Linux服务器_入门
http://www.oschina.net/code/snippet_222919_11734 代码分享 当前位置: 代码分享 » Java » 网络编程 搜 索 [饶过] 使用Java管理千 ...
- Django之路:QuerySet API,后台和表单
一.Django QuerySet API Django模型中我们学习了一些基本的创建和查询.这里专门讲以下数据库接口相关的接口(QuerySet API),当然你也可以选择暂时跳过这节.如果以后用到 ...