五道水题,但要手快才好。。。我手慢了,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 ;
}

Codeforces Round #303 (Div. 2) E的更多相关文章

  1. 水题 Codeforces Round #303 (Div. 2) D. Queue

    题目传送门 /* 比C还水... */ #include <cstdio> #include <algorithm> #include <cstring> #inc ...

  2. DP Codeforces Round #303 (Div. 2) C. Woodcutters

    题目传送门 /* 题意:每棵树给出坐标和高度,可以往左右倒,也可以不倒 问最多能砍到多少棵树 DP:dp[i][0/1/2] 表示到了第i棵树时,它倒左或右或不动能倒多少棵树 分情况讨论,若符合就取最 ...

  3. 贪心 Codeforces Round #303 (Div. 2) B. Equidistant String

    题目传送门 /* 题意:找到一个字符串p,使得它和s,t的不同的总个数相同 贪心:假设p与s相同,奇偶变换赋值,当是偶数,则有答案 */ #include <cstdio> #includ ...

  4. 水题 Codeforces Round #303 (Div. 2) A. Toy Cars

    题目传送门 /* 题意:5种情况对应对应第i或j辆车翻了没 水题:其实就看对角线的上半边就可以了,vis判断,可惜WA了一次 3: if both cars turned over during th ...

  5. Codeforces Round #303 (Div. 2) D. Queue 傻逼题

    C. Woodcutters Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/545/probl ...

  6. Codeforces Round #303 (Div. 2) C. Woodcutters 贪心

    C. Woodcutters Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/545/probl ...

  7. Codeforces Round #303 (Div. 2) B. Equidistant String 水题

    B. Equidistant String Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/54 ...

  8. Codeforces Round #303 (Div. 2) A. Toy Cars 水题

     A. Toy Cars Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/545/problem ...

  9. 「日常训练」Queue(Codeforces Round 303 Div.2 D)

    简单到让人不敢相信是D题,但是还是疏忽了一点. 题意与分析 (Codeforces 545D) 题意:n人排队,当一个人排队的时间超过他需要服务的时间就会厌烦,现在要求一个最优排列使得厌烦的人最少. ...

  10. 「日常训练」Woodcutters(Codeforces Round 303 Div.2 C)

    这题惨遭被卡..卡了一个小时,太真实了. 题意与分析 (Codeforces 545C) 题意:给定\(n\)棵树,在\(x\)位置,高为\(h\),然后可以左倒右倒,然后倒下去会占据\([x-h,x ...

随机推荐

  1. Unity基本操作

    主要内容: C#学习 Unity项目 打砖块:BreakBricks Unity操作 Unity操作: 调试 碰撞体 触发器 视角 键盘视角平移 光照贴图 游戏对象Gameobject 访问对象 实体 ...

  2. vue-resource 拦截器的使用

    园友参考  https://www.cnblogs.com/lhl66/p/8022823.html vue-resource 拦截器使用在vue项目使用vue-resource的过程中,临时增加了一 ...

  3. Java基础学习经验分享

    很多人学习Java,尤其是自学的人,在学习的过程中会遇到各种各样的问题以及难点,有时候卡在一个点上可能需要很长时间,因为你在自学的过程中不知道如何去掌握和灵活运用以及该注意的点.下面我整理了新手学习可 ...

  4. Netty(2) - HelloWorld

    Netty:作用场景. 1)Netty可以基于socket实现远程过程调用(RPC). 2)Netty可以基于WebSocket实现长连接. 3)Netty可以实现Http的服务器,类似于Jetty, ...

  5. [Luogu 2678] noip15 子串

    [Luogu 2678] noip15 子串 题目描述 有两个仅包含小写英文字母的字符串 A 和 B.现在要从字符串 A 中取出 k 个互不重叠的非空子串,然后把这 k 个子串按照其在字符串 A 中出 ...

  6. mybatis中打印sql语句

    在mybatis-config.xml中properties节点下,配置一个settings节点 <settings> <setting name="cacheEnable ...

  7. jq-文本框只能输入数字

    <input type="text" onKeyUp="value=value.replace(/\D/g,'')"  /> onKeyUp: 当输 ...

  8. 【转】Java 集合系列02之 Collection架构

    概要 首先,我们对Collection进行说明.下面先看看Collection的一些框架类的关系图: Collection是一个接口,它主要的两个分支是:List 和 Set. List和Set都是接 ...

  9. SAS进阶《深入解析SAS》之开发多语言支持的SAS程序

    SAS进阶<深入解析SAS>之开发多语言支持的SAS程序 1. 多语言支持的应用程序是指该程序在世界给第使用时,其能够处理的数据,以及处理数据的方式.信息展现的方式都符合当地的语言.文化习 ...

  10. dubbo之本地伪装

    本地伪装 本地伪装 1 通常用于服务降级,比如某验权服务,当服务提供方全部挂掉后,客户端不抛出异常,而是通过 Mock 数据返回授权失败. 在 spring 配置文件中按以下方式配置: <dub ...