NC236210 传送门
题目链接
关键点:
1、建图:对于同一类型的传送门,各点间可以连接ti长度的边,但是如果对于给出的点都两两连边,会导致复杂度过大,我们可以考虑对于每一种类型的传送门,设置一个总点,使该类型的点直接与该点连ti长度的双向边,保证同一类型的传送门的点之间,一定要通过该总点才能访问。那么这就可以保证同一类型的点之间的长度为ti
2、答案会很大,要开longlong,且dis的初始化最好要大一点
完整代码:
# include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 100000*3+10;
struct ty{
int to, next, len;
}edge[10000000+10];
struct ty2{
int x, len;
bool operator < (const ty2& a)const{
return len>a.len;
}
};
int n, m, cnt;
ll dis[N], vis[N], head[N], c[N];
priority_queue<ty2> q;
void addedge(int x, int y, int len)
{
edge[++cnt].to = y;
edge[cnt].len = len;
edge[cnt].next = head[x];
head[x] = cnt;
}
void dij(int s)
{
memset(vis, 0, sizeof(vis));
memset(dis, 0x3f, sizeof(dis));
dis[s] = 0;
ty2 tmp;
tmp.len = 0, tmp.x = s;
q.push(tmp);
while (!q.empty())
{
ty2 tmp = q.top();
q.pop();
if (vis[tmp.x]) continue;
vis[tmp.x] = 1;
for (int i=head[tmp.x]; i!=-1; i=edge[i].next)
{
int y = edge[i].to;
if (vis[y]) continue;
if (dis[tmp.x] + edge[i].len < dis[y])
{
dis[y] = dis[tmp.x] + edge[i].len;
ty2 tmp;
tmp.x = y, tmp.len = dis[y];
q.push(tmp);
}
}
}
}
int main()
{
cin>>n>>m;
memset(head, -1, sizeof(head));
for (int i=1; i<=m; i++)
{
int ti, si;
cin>>ti>>si;
for (int j=1; j<=si; j++)
{
int t;
cin>>t;
addedge(i+100000, t, ti);
addedge(t, i+100000, ti);
}
}
dij(1);
memcpy(c, dis, sizeof(dis));
dij(n);
ll ans = 0x3f3f3f3f;
for (int i=1; i<=n; i++)
{
if (dis[i]!=0x3f3f3f&&c[i]!=0x3f3f3f3f)
{
ans = min(ans, max(dis[i], c[i]));
}
}
if (ans!=0x3f3f3f3f)
{
cout<<ans/2<<endl;
for (int i=1; i<=n; i++)
{
if (ans == max(dis[i], c[i]))
cout<<i<<" ";
}
}
else
cout<<"-1"<<endl;
return 0;
}