五道水题,但要手快才好。。。我手慢了,E题目都没看完TAT....

想了一发,很水,就是一遍Dijk即可,使用优先队列,同时记录由哪条边转移而来

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#define LL long long
using namespace std; const int MAXN=; struct Edge{
int u,v,w,i,next;
}edge[MAXN*];
int weight[MAXN],head[MAXN],tot;
int n,m; int chose[MAXN];
LL dis[MAXN]; bool vis[MAXN]; struct Node{
int u; LL d;
Node(){}
Node(int uu,LL dd){u=uu,d=dd;}
bool operator<(const Node &a)const{
return d>a.d;
}
}; vector <int>ans;
priority_queue<Node>pq;
LL cur; void addedge(int u,int v,int w,int i){
edge[tot].u=u; edge[tot].v=v;
edge[tot].w=w; edge[tot].i=i;
edge[tot].next=head[u];
head[u]=tot++;
} void Dijk(int r){
cur=;
for(int i=;i<=n;i++) dis[i]=1ll<<;
dis[r]=;
memset(chose,,sizeof(chose));
memset(vis,false,sizeof(vis));
ans.clear();
pq.push(Node(r,));
while(!pq.empty()){
Node tmp=pq.top(); pq.pop();
if(vis[tmp.u]) continue;
if(tmp.u!=r){
cur+=weight[chose[tmp.u]];
ans.push_back(chose[tmp.u]);
}
LL d=tmp.d;
vis[tmp.u]=true;
for(int e=head[tmp.u];e!=-;e=edge[e].next){
int v=edge[e].v;
if(!vis[v]){
if(d+edge[e].w<dis[v]){
dis[v]=d+edge[e].w;
chose[v]=edge[e].i;
pq.push(Node(v,dis[v]));
}
else if(d+edge[e].w==dis[v]){
if(edge[e].w<weight[chose[v]]){
chose[v]=edge[e].i;
}
}
}
}
}
sort(ans.begin(),ans.end());
cout<<cur<<endl;
int len=ans.size();
if(len>){
printf("%d",ans[]);
for(int i=;i<len;i++)
printf(" %d",ans[i]);
printf("\n");
}
} int main(){
int u,v,w;
while(scanf("%d%d",&n,&m)!=EOF){
memset(head,-,sizeof(int)*(n+));
tot=;
weight[]=1e9+;
for(int i=;i<=m;i++){
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w,i);
addedge(v,u,w,i);
weight[i]=w;
}
scanf("%d",&u);
Dijk(u);
}
return ;
}

最新文章

  1. java异步式Socket响应数据获取方案
  2. HTTP状态代码含义
  3. Android 7.0 UICC 分析(一)
  4. Linux shell基础
  5. 个人建了一个APPCAN移动前端开发交流QQ群258213194
  6. 智能车学习(一)&mdash;&mdash; 硬件准备
  7. oracle数据库ORA-01654 错误的解决方法
  8. js----方法是否加括号的问题
  9. &#39;mysql&#39; 不是内部或外部命令,也不是可运行的程序或批处理文件的解决办法
  10. C# zip/unzip with ICSharpCode.SharpZipLib
  11. 如何唯一确定一台iOS设备
  12. 面向对象(metaclass继承高级用法)
  13. maven打包时跳过单元测试
  14. 一图看懂Spring获取对象与java new对象区别
  15. java泛型介绍
  16. Spring学习九 Servlet相关
  17. Windows核心编程-作业
  18. Excel 文件下载
  19. [转]PNG8和PNG24的区别
  20. CentOS 7使用dnf安装Memcached以及启动、停止、开机启动等设置

热门文章

  1. POJ 1584 计算几何
  2. 如何查看jdk的版本
  3. Coursera公开课-Machine_learing:编程作业3
  4. iOS - UITableView 多选功能实现
  5. Android 升级安装APK兼容Android7.0,解决FileUriExposedException
  6. 编写第一个HTML5文件
  7. Android 基础知识图谱
  8. [Windows Server 2012] 服务器安全加固
  9. struts2_validate表单验证
  10. Random同时生成多个随机数