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. 笨方法学python--简介

    该章提到编程新手所需的三种最重要的技术:读和写,注重细节,发现不同. 读 和 写 即熟悉代码中的各种字符. 注 重 细 节 将例子一字不差地打出来,通过实践训练自己 发 现 不 同 这个是通过长年累月 ...

  2. C#生成随机汉字

    using System; using System.Text; namespace ConsoleApplication {     class ChineseCode     {          ...

  3. HDU 5455 Fang Fang 水题,但题意描述有问题

    题目大意:f[1]=f,f[2]=ff,f[3]=ffc,以后f[n]每增加1,字符串增加一个c.给出一个字符串,求最少有多少个f[]组成.(字符串首尾相连,比如:ffcf可看做cfff) 题目思路: ...

  4. IE 和 FF 写不同的CSS

    .FireFox 下如何使连续长字段自动换行 众所周知IE中直接使用word-wrap:break-word 就可以了, FF中我们使用JS插入的技巧来解决 <style type=" ...

  5. ajax遇到的问题

    今天做了个小小的实验,用ajax XMLHttpRequest对象读取服务器上的txt文件里的内容,展示出来 直接把html文件放在桌面用浏览器打开,没有反应,部分代码如下: function oHt ...

  6. hdu 3345 War Chess

    War Chess Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Sub ...

  7. ubuntu11.10server 安装php-redis插件

    1.下载php-redis插件 1 sudo wget https://github.com/nicolasff/phpredis/archive/master.zip 2.安装 1 2 3 4 5 ...

  8. 转:Selenium-Grid工作方式

    Selenium-Grid版本 selenium-grid分为版本1和版本2,其实它的2个版本并不是和selenium的版本1和2相对应发布的[即selenium-grid2的发布比selenium2 ...

  9. C# Excel嵌入到Winform

    本文讲的这个技术是把Excel表格嵌入到自己开发程序的Form窗体中进行操作,给客户一个不用切换窗口的操作界面,似乎更好.这在VC中用OLE技术很容易实现,但是在C#中方法就不一样啦.下面将就此进行阐 ...

  10. MYSQL启用日志,查看日志,利用mysqlbinlog工具恢复MySQL数据库【转载】

    转自 MYSQL启用日志,查看日志,利用mysqlbinlog工具恢复MySQL数据库 - _安静 - 博客园http://www.cnblogs.com/xionghui/archive/2012/ ...