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. FreeRTOS中断优先级配置(重要)

    FreeRTOS中断优先级配置(重要) 本章节为大家讲解FreeRTOS中断优先级配置,此章节非常重要,初学者经常在这里犯迷糊.对于初学者来说,本章节务必要整明白.12.1 NVIC基础知识12.2  ...

  2. selenium2.0集成测试案例

    webDriver模拟点击对web工程测试还是挺方便的. package suite; import java.util.concurrent.TimeUnit; import org.junit.A ...

  3. Day02——widow对象

    window - 计时器 1、setTimeout()可以用来在指定的时间之后单次调用函数. setTimeount(f,1000);//一秒后调用函数f clearTimeout();取消函数的执行 ...

  4. 为ubuntu添加右键打开终端效果

    安装工具:nautilus-open-terminal sudo apt-get install nautilus-open-terminal 注销或重启

  5. c++ 日志操作

    程序需要一个简单的日志类,为此简单学习了Boost.Log和google的glog,前者功能非常强大,后者非常小巧但是不够灵活,最终打算自己写一个. 环境: win7 32位旗舰版.VS2010旗舰版 ...

  6. linux视频学习7(ssh, linux启动过程分析,加解压缩,java网络编程)

    回顾数据库mysql的备份和恢复: show databases; user spdb1; show tables; 在mysql/bin目录下 执行备份: ./mysqldump -u root - ...

  7. Android简单逐帧动画Frame的实现(三)

    android之动画(三)通过AnimationDrawable控制逐帧动画     android与逐帧动画: 效果图: 当我们点击按钮时,该图片会不停的旋转,当再次点击按钮时,会停止在当前的状态. ...

  8. HBase性能优化方法总结(一):表的设计

    本文主要是从HBase应用程序设计与开发的角度,总结几种常用的性能优化方法.有关HBase系统配置级别的优化,可参考:淘宝Ken Wu同学的博客. 下面是本文总结的第一部分内容:表的设计相关的优化方法 ...

  9. 学习笔记——抽象工厂模式Abstract Factory

    在工厂模式的基础上,通过为工厂类增加接口,实现其他产品的生产,而不用一类产品就增加一个工厂. 依然以<真菌世界>游戏故事类比,树作为工厂,如果现在有两类树,一类生产快速弄真菌飞机和20毫米 ...

  10. PAT (Advanced Level) 1113. Integer Set Partition (25)

    简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...