CreeperLordVader @ 2018-10-19 19:26:08
花了好久了,求助啊
#include<bits/stdc++.h>
using namespace std;
int n,m,ans,cnt;
int d[100010];
int edge[100010],son[100010];
int c[100010];
bool b[100010];
bool jud;
queue<int>q;
vector<int>v[100010];
vector<int>e[100010];
bool read(int& x)
{
char c=getchar();
x=0;
while(c<'0'||c>'9')
{
if(c==EOF)return 0;
c=getchar();
}
while(c>='0'&&c<='9')
{
x=x*10+c-'0';
c=getchar();
}
return 1;
}
void bfs(int s)
{
d[s]=0;
q.push(s);
b[s]=1;
while(!q.empty())
{
int fa=q.front();
q.pop();
// cout<<fa<<endl;
for(int i=0; i<v[fa].size(); i++)
{
int y=v[fa][i];
if(b[y])continue;
b[y]=1;
d[y]=d[fa]+1;
q.push(y);
}
}
}
void bfs2(int s)
{
while(!q.empty())
{
int fa=q.front();
q.pop();
int minn=edge[fa];
int nxt=son[fa];
if(fa==n)return ;
for(int i=0;i<v[fa].size();i++)
{
int y=v[fa][i];
int z=e[fa][i];
if(d[fa]-d[y]==1)
{
if(minn==z)
{
if(edge[y]<edge[nxt])
{
nxt=y;
}
}
}
}
c[++cnt]=minn;
q.push(nxt);
}
}
int main()
{
while(read(n)&&read(m))
{
ans=0;
while(!q.empty())q.pop();
for(int i=1;i<=n;i++)
{
v[i].clear();
e[i].clear();
}
memset(b,0,sizeof(b));
memset(d,0,sizeof(d));
memset(edge,0x3f3f3f3f,sizeof(edge));
memset(son,0,sizeof(son));
for(int i=1; i<=m; i++)
{
int x,y,z;
read(x);
read(y);
read(z);
v[x].push_back(y);
v[y].push_back(x);
e[x].push_back(z);
e[y].push_back(z);
}
bfs(n);
printf("%d\n",d[1]);
for(int i=1;i<=n;i++)
{
for(int j=0;j<v[i].size();j++)
{
int z=e[i][j];
int y=v[i][j];
if(edge[i]>z&&d[i]-d[y]==1)
{
edge[i]=z;
son[i]=y;
}
}
}
q.push(1);
bfs2(1);
for(int i=1;i<=d[1];i++)
{
if(i!=d[1])
printf("%d ",c[i]);
else
printf("%d",c[i]);
}
putchar('\n');
}
}