You are given an undirected connected weighted graph consisting of nn vertices and mmedges. Let's denote the length of the shortest path from vertex 11 to vertex ii as didi.
You have to erase some edges of the graph so that at most kk edges remain. Let's call a vertex ii good if there still exists a path from 11 to ii with length didi after erasing the edges.
Your goal is to erase the edges in such a way that the number of good vertices is maximized.
Input
The first line contains three integers nn, mm and kk (2≤n≤3?1052≤n≤3?105, 1≤m≤3?1051≤m≤3?105, n?1≤mn?1≤m, 0≤k≤m0≤k≤m) — the number of vertices and edges in the graph, and the maximum number of edges that can be retained in the graph, respectively.
Then mm lines follow, each containing three integers xx, yy, ww (1≤x,y≤n1≤x,y≤n, x≠yx≠y, 1≤w≤1091≤w≤109), denoting an edge connecting vertices xx and yy and having weight ww.
The given graph is connected (any vertex can be reached from any other vertex) and simple (there are no self-loops, and for each unordered pair of vertices there exists at most one edge connecting these vertices).
Output
In the first line print ee — the number of edges that should remain in the graph (0≤e≤k0≤e≤k).
In the second line print ee distinct integers from 11 to mm — the indices of edges that should remain in the graph. Edges are numbered in the same order they are given in the input. The number of good vertices should be as large as possible.
Examples
Input
3 3 2 1 2 1 3 2 1 1 3 3
Output
2 1 2
Input
4 5 2 4 1 8 2 4 1 2 1 3 3 4 9 3 1 5
Output
2 3 2
【CodeForces - 1076D Edge Deletion 最短路标记边】题意:n个点 m个边 取最多k个边 最多可以取多少点并且保证1到各点的最短路不变
题解:跑一边最短路,记录每个点最后是通过哪个边更新到的,每次到达一个点,把得到他的边记录下来即可
不知道为什么在优先队列不能用dis数组判断 一直错在第7个 坑死
#include
using namespace std;
typedef long long ll;
const int N=3e5+10;
#define INF 1e18+10
int n,m,k;
int pid[N];
ll dis[N];
bool vis[N];
struct node1{
int to,id;
ll d;
node1(int to_,ll d_,int id_):to(to_),d(d_),id(id_){}
};
struct node2{
int id;
ll l;
node2(int id_,ll l_):id(id_),l(l_){}
bool operator <(const node2 &x)const{
return l>x.l;
}
};
vector v1[N];
int cnt,ans[N];
void DIJ()
{
for(int i=1;
i<=n;
i++) dis[i]=INF,vis[i]=0;
dis[1]=0;
priority_queue q;
q.push(node2(1,0));
cnt=-1;
while(!q.empty())
{
node2 now=q.top();
q.pop();
if(vis[now.id]) continue;
vis[now.id]=1;
ans[++cnt]=pid[now.id];
if(cnt==k) return;
for(int i=0;
idis[now.id]+v1[now.id][i].d)
{
dis[to]=dis[now.id]+v1[now.id][i].d;
pid[to]=v1[now.id][i].id;
q.push(node2(to,dis[to]));
}
}
}
}
int main()
{
int x,y;
ll z;
scanf("%d%d%d",&n,&m,&k);
for(int i=1;
i<=m;
i++)
{
scanf("%d%d%lld",&x,&y,&z);
v1[x].push_back(node1(y,z,i));
v1[y].push_back(node1(x,z,i));
}
k=min(k,n-1);
DIJ();
printf("%d\n",k);
if(k)
{
for(int i=1;
i<=k;
i++)
printf("%d%c",ans[i]," \n"[i==k]);
}
else printf("\n");
return 0;
}
推荐阅读
- 次短路|SSLOJ 1297.GF打Dota
- 图论|POJ1364 King 差分约束
- 拆位+堆优化最短路 牛妹游历城市 牛客练习赛67
- PAT|(pat)A1030. Travel Plan
- KD-Tree|【NOI2019】【LOJ3259】【洛谷P5471】弹跳(K-D Tree)(最短路)
- #|LOJ#3156. 「NOI2019」回家路线(前缀和优化建图+for循环+凸包)
- hdu1690
- 最短路|poj1511+链式前向星+dijkstra堆优化
- POJ2240 spfa判增大环 poj3259 spfa判负环