列表

详情


NC24433. Ramen Network Protocol

描述

You should have been heard of TCP and UDP. They are the most popular network protocols currently.

Recently, noodles, or more specifically, Ramen noodles, are discovered as a great material to make cables. And the new cable which is made from Ramen noodles is called Ramen Fiber. Ramen is a researcher of the Academy of Communication Mechanism and the core inventor of Ramen Fiber.

He also designs a new protocol which relies upon the exciting Ramen Fiber. The new protocol is named Ramen Network Protocol(RNP). In many aspects, the RNP works just like TCP and UDP. But here is one significant difference: the transmission between all Ramen Fibers is lossless, which means a packet will reach its destination while losing nothing. Why? It's the Ramen Power!

To measure the performance of RNP, Ramen wants to calculate the shortest time from any sender to any receiver if the packet has been transferred most optimally. He has set lots of senders and receivers, and an RNP powered LAN. But he is not good at writing benchmarks. As a good researcher as well as Ramen, can you help him with this tiny work to earn a nice meal?

输入描述

The input contains multiple lines.

The first line is four integers: n, m, s, t(1 <= n <= 1e4, 1 <= m <= 5e4, 1 <= s,t <= n, s+t <= n), represents the number of nodes, RFs, senders and receivers.

The second line contains exactly s integers S1...Ss, each represents a distinct sender, for all 1 <= i <= s, 1 <= Si <= n.

The third line contains exactly t integers T1...Tt, each represents a distinct receiver, for all 1 <= i <= t, 1 <= Ti <= n.

Each of the next m lines contains three integers: u, v, w(1 <= u, v <= n, u ≠ v, 1 <= w <= 1e9), means that there is an RF between node u to node v and the time needed for a packet to go through it. All RFs are distinct.

It's guaranteed that no point is a sender and a receiver simultaneously.

输出描述

If there is no packet can reach any receiver, output -1. Otherwise, output the shortest time in one single line.

示例1

输入:

10 14 3 3
1 4 9
6 8 10
1 2 12
2 3 4
2 8 4
3 4 5
3 5 5
3 8 9
3 9 1
3 10 8
4 5 2
5 6 7
5 7 6
6 7 14
7 9 11
7 10 23

输出:

9

说明:

There are three routes will produce the shortest time: 4\to5\to69\to3\to2\to8 and 9\to3\to10.

示例2

输入:

4 2 2 2
1 2
3 4
1 2 1
3 4 1

输出:

-1

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++14(g++5.4) 解法, 执行用时: 21ms, 内存消耗: 4328K, 提交时间: 2019-04-14 12:24:23

#include<bits/stdc++.h>
#define N 100005
#define int long long
#define inf 2e16
using namespace std;

inline void read(int &X)
{
    X=0;int w=0;char ch=0;
    while(!isdigit(ch))w|=ch=='-',ch=getchar();
    while( isdigit(ch))X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
    X=w?-X:X;
}
int n,m,s,t;
int head[N],cnt,dis[N],v[N];
struct nd{int next,to,v;}e[N*5];

void add(int x,int y,int v)
{
    e[++cnt]=(nd){head[x],y,v};head[x]=cnt;
}
void dj(int s)
{
    memset(v,0,sizeof v);
    priority_queue< pair<int,int> > q;
    for(int i=1;i<=n+2;i++) dis[i]=inf;
    dis[s]=0;q.push(make_pair(0,s));
    while(!q.empty())
    {
        int x=q.top().second;q.pop();
        if(v[x]) continue;v[x]=1;
        for(int y,i=head[x];i;i=e[i].next)
            if(dis[y=e[i].to]>dis[x]+e[i].v)
                dis[y]=dis[x]+e[i].v,q.push(make_pair(-dis[y],y));
    }

}
signed main()
{
    scanf("%lld%lld%lld%lld",&n,&m,&s,&t);
    for(int x,i=1;i<=s;i++)
        read(x),add(n+1,x,0);
    for(int x,i=1;i<=t;i++)
        read(x),add(x,n+2,0);
    for(int x,y,v,i=1;i<=m;i++)
        read(x),read(y),read(v),add(x,y,v),add(y,x,v);
    dj(n+1);
    if(dis[n+2]==inf) puts("-1");
    else cout<<dis[n+2];
}

C++ 解法, 执行用时: 68ms, 内存消耗: 7272K, 提交时间: 2022-07-13 00:35:31

#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f3f3f
typedef long long ll;
struct Edge
{
	ll to;
	ll nxt;
	ll w;
}edge[1000010];
ll tot=0,head[100010],vis[100010]; 
void add(ll u,ll v,ll w)
{
	edge[++tot].to=v; edge[tot].nxt=head[u]; edge[tot].w=w; head[u]=tot;
	edge[++tot].to=u; edge[tot].nxt=head[v]; edge[tot].w=w; head[v]=tot;
}
ll n,m,s,t,inq[210100],dis[210100];
void spfa(int u)
{
	memset(dis,INF,sizeof(dis));
	memset(inq, 0 ,sizeof(inq));
	queue<int>q;
	q.push(u);inq[u]=1;dis[u]=0;
	while(!q.empty())
	{
		int t=q.front();
		q.pop(); inq[t]=0;
		for(int i=head[t];~i;i=edge[i].nxt)
		{
			Edge e=edge[i];
			if(dis[e.to]>dis[t]+e.w)
			{
				dis[e.to]=dis[t]+e.w;
				if(!inq[e.to])
				{
					inq[e.to]=1;
					q.push(e.to);
				}
			}
		} 
	}
}
int main()
{
	memset(head,-1,sizeof(head));
	cin>>n>>m>>s>>t;
	for(int i=1;i<=s;i++) 
	{
		ll s; cin>>s;
		add(0,s,0);
	}
	for(int i=0;i<t;i++) 
	{
		ll t; cin>>t;
		add(t,n+1,0);
	}
	for(int i=1;i<=m;i++)
	{
		ll u,v,w; cin>>u>>v>>w;
		add(u,v,w);
	}
	spfa(0);
	if(dis[n+1]-INF<1) cout<<dis[n+1]<<endl;
	else cout<<"-1"<<endl;
	return 0;
}

上一题