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的更多相关文章

  1. 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 ...

  2. zoj 3946 Highway Project(最短路 + 优先队列)

    Highway Project Time Limit: 2 Seconds      Memory Limit: 65536 KB Edward, the emperor of the Marjar ...

  3. ZOJ 3946 Highway Project(Dijkstra)

    Highway Project Time Limit: 2 Seconds      Memory Limit: 65536 KB Edward, the emperor of the Marjar ...

  4. ZOJ 3946 Highway Project 贪心+最短路

    题目链接: http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=3946 题解: 用dijkstra跑单元最短路径,如果对于顶点v,存 ...

  5. ZOJ 3946 Highway Project (最短路)

    题意:单源最短路,给你一些路,给你这些路的长度,给你修这些路的话费,求最短路和最小花费. 析:本质就是一个最短路,不过要维护两个值罢了,在维护花费时要维护的是该路要花多少,而不是总的路线花费. 代码如 ...

  6. (spfa) Highway Project (zoj 3946 )

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5718   Highway Project Time Limit: 2 Seco ...

  7. ZOJ3946:Highway Project(最短路变形)

    本文转载自:http://www.javaxxz.com/thread-359442-1-1.html Edward, the emperor of the Marjar Empire, wants ...

  8. ZOJ-3946 Highway Project (最短路)

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

  9. The 13th Zhejiang Provincial Collegiate Contest(2016年浙江省赛)

      前4道水题就不说了,其中我做了C题,1Y,小心仔细写代码并且提交之前得确认无误后提交才能减少出错率. 结果后面2题都由波神做掉,学长带我们飞~ 终榜 官方题解   ZOJ 3946 Highway ...

随机推荐

  1. mongostat

    监控MongoDB有一下几种方法: 1)使用MongoDB软件包自带的工具,如mongostat,mongotop等.2)使用MongoDB数据库命令3)使用MongoDB公司提供的MMS监控服务 m ...

  2. window窗体程序意外崩溃,EventType clr20r3错误的解决方法

    EventType clr20r3, P1 ggreadcard.exe, P2 1.0.0.0, P3 51d3d283, P4 zljy.common, P5 1.0.0.0, P6 4fc6c2 ...

  3. Java学习笔记之Scanner报错java.util.NoSuchElementException

    转载自:IT学习者-螃蟹 一个方法A使用了Scanner,在里面把它关闭了.然后又在方法B里调用方法A之后就不能再用Scanner了Scanner in = new Scanner(System.in ...

  4. git repository 的使用

    1. You should only be pushing to a bare repository. A bare repository is a repository that has no ch ...

  5. js中Object.__proto__===Function.prototype

    参考:http://stackoverflow.com/questions/650764/how-does-proto-differ-from-constructor-prototype http:/ ...

  6. Games on a CD

    Games on a CD time limit per test 4 seconds memory limit per test 512 megabytes input standard input ...

  7. UIScreen的scale属性

    用来表示显示屏的像素密度与点坐标系统之间的关系.通过该属性,我们可以把视图中逻辑坐标系统里的点坐标转换成设备的物理像素坐标.在配有Retina显示屏的设备中,scale值是2.0,而在非Retina显 ...

  8. php 判断是不是https链接

    如果当前运行的 PHP 版本等于或高于提供的版本号,该函数返回布尔值 TRUE ,反之则返回 FALSE . function is_https() { if (!empty($_SERVER['HT ...

  9. redis 队列缓存 + mysql 批量入库 + php 离线整合

    问题分析 思考:应用网站架构的衍化过程中,应用最新的框架和工具技术固然是最优选择:但是,如果能在现有的框架的基础上提出简单可依赖的解决方案,未尝不是一种提升自我的尝试. 解决: 问题一:要求日志最好入 ...

  10. Extjs 4.1 struts2.3 返回json 初试

    之前曾经使用过3.x版本的extjs,当时可以结合struts实现各种基本的增删查改.但是4.1版本中增加了一些属性,出现了一些新的使用方法,导致错误不断,有的时候调用到相应的action却返回不了值 ...