五道水题,但要手快才好。。。我手慢了,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. akka监控

    使用akka系统时间就了,你就一定会想着监控的事儿.比如某个actor发送了多少消息.接收了多少消息.消息平均处理时间是多少,当前有多少个actor等等.本来我都用bytebuddy写了个简单的akk ...

  2. C# 工厂单例

     public class BusinessFactory    {        private static BusinessFactory instance = null;        pri ...

  3. 数塔问题mod 100(orz)

    看一下题目 和普通的数字三角形看似没啥区别(区别很大) 然后去想:DP方程 DP[i][j]=Max(DP[i-][j],DP[i-][j-])+a[i][j] ans=Max(DP[n][..n]) ...

  4. 取消安卓listview,scrollview,gridview滑动时候边缘模糊问题

    只需在xml文件里面声明: android:faddingEdge = "none" android:faddingEdgelenth = "0dp" andr ...

  5. BZOJ 4310 二分+SA+RMQ

    思路: 首先求出后缀数组和height数组,这样能得到本质不同的子串数目 这里利用:本质不同的子串=∑(Len−SA[i]−height[i])=∑(Len−SA[i]−height[i])利用SA[ ...

  6. 【LeetCode】467. Unique Substrings in Wraparound String

    Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...

  7. Spark 概念学习系列之Spark基本概念和模型(十八)

    打好基础,别小瞧它! spark的运行模式多种多样,在单机上既可以本地模式运行,也可以伪分布模式运行.而当以分布式的方式在集群中运行时.底层的资源调度可以使用Mesos或者Yarn,也可使用spark ...

  8. Echarts 出现不明竖线解决方案

    Echarts出现了不明竖线,百思不得其解.去查相应的解决方案也没有找到. 后来自己点来点去,突然感觉像是上一个Echarts遗留的. 然后去Echarts官网看到了 clear()方法,这个方法可以 ...

  9. mysql外键创建失败原因

    引用:http://blog.csdn.net/wangpeng047/article/details/19624351 首先,如果和外键相关的几张表中已经插入了数据,可能导致外键插入的失败 在MyS ...

  10. Failed to resolve com.android.support:support-annotations 26.0.1

    所有当前版本的Google库都存放在 Google的Maven repository (maven.google.com),不在旧的offline-capable support repositori ...