列表

详情


NC51292. Sightseeing

描述

Tour operator Your Personal Holiday organises guided bus trips across the Benelux. Every day the bus moves from one city S to another city F. On this way, the tourists in the bus can see the sights alongside the route travelled. Moreover, the bus makes a number of stops (zero or more) at some beautiful cities, where the tourists get out to see the local sights.

Different groups of tourists may have different preferences for the sights they want to see, and thus for the route to be taken from S to F. Therefore, Your Personal Holiday wants to offer its clients a choice from many different routes. As hotels have been booked in advance, the starting city S and the final city F, though, are fixed. Two routes from S to F are considered different if there is at least one road from a city A to a city B which is part of one route, but not of the other route.

There is a restriction on the routes that the tourists may choose from. To leave enough time for the sightseeing at the stops (and to avoid using too much fuel), the bus has to take a short route from S to F. It has to be either a route with minimal distance, or a route which is one distance unit longer than the minimal distance. Indeed, by allowing routes that are one distance unit longer, the tourists may have more choice than by restricting them to exactly the minimal routes. This enhances the impression of a personal holiday.

For example, for the above road map, there are two minimal routes from S = 1 to F = 5: 1 → 2 → 5 and 1 → 3 → 5, both of length 6. There is one route that is one distance unit longer: 1 → 3 → 4 → 5, of length 7.

Now, given a (partial) road map of the Benelux and two cities S and F, tour operator Your Personal Holiday likes to know how many different routes it can offer to its clients, under the above restriction on the route length.

输入描述

The first line of the input file contains a single number: the number of test cases to follow. Each test case has the following format:
One line with two integers N and M, separated by a single space, with and : the number of cities and the number of roads in the road map.
M lines, each with three integers A, B and L, separated by single spaces, with , and , describing a road from city A to city B with length L.
The roads are unidirectional. Hence, if there is a road from A to B, then there is not necessarily also a road from B to A. There may be different roads from a city A to a city B.
One line with two integers S and F, separated by a single space, with , and : the starting city and the final city of the route.
There will be at least one route from S to F.

输出描述

For every test case in the input file, the output should contain a single number, on a single line: the number of routes of minimal length or one distance unit longer. Test cases are such, that this number is at most 

示例1

输入:

2
5 8
1 2 3
1 3 2
1 4 5
2 3 1
2 5 3
3 4 2
3 5 4
4 5 3
1 5
5 6
2 3 1
3 2 1
3 1 10
4 5 2
5 2 7
5 2 7
4 1

输出:

3
2

原站题解

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

C++(g++ 7.5.0) 解法, 执行用时: 13ms, 内存消耗: 648K, 提交时间: 2022-08-20 10:49:44

#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 10;
const int M = 1e4 + 10;
struct edge {
    int to, next, vi;
}e[M << 1];
int n, m, h[N], cnt, s, t, vis[2][N];
int dis[2][N], ans[2][N];
void add(int u, int v, int w) {
    e[cnt].to = v;
    e[cnt].vi = w;
    e[cnt].next = h[u];
    h[u] = cnt++;
}
struct node {
    int id;
    int d;
    int now;
    bool operator < (const node& x) const {
        return d > x.d;
    }
};
void dij()
{
    priority_queue<node> q;
    memset(dis, 0x3f, sizeof dis);
    memset(vis, 0, sizeof vis);
    memset(ans, 0, sizeof ans);
 
    dis[0][s] = 0, ans[0][s] = 1;
    q.push({ 0, dis[0][s], s });
    while (q.size()) {
        int id = q.top().id;
        int u = q.top().now;
        q.pop();
        if (vis[id][u]) continue;
        vis[id][u] = 1;
        for (int i = h[u]; ~i; i = e[i].next) {
            int v = e[i].to;
            int tmp = dis[id][u] + e[i].vi;
            if (dis[0][v] > tmp) {
                dis[1][v] = dis[0][v];
                ans[1][v] = ans[0][v];
 
                dis[0][v] = tmp;
                ans[0][v] = ans[id][u];
                q.push({ 0, dis[0][v], v });
                q.push({ 1, dis[1][v], v });
            }
            else if (dis[0][v] == tmp) {
                ans[0][v] += ans[id][u];
            }
            else if (dis[1][v] > tmp) {
                dis[1][v] = tmp;
                ans[1][v] = ans[id][u];
                q.push({ 1, dis[1][v], v });
            }
            else if (dis[1][v] == tmp) {
                ans[1][v] += ans[id][u];
            }
        }
    }
}
 
int main() {
    int T;
    scanf("%d", &T);
    while (T--) {
        memset(h, -1, sizeof h);
        cnt = 0;
        scanf("%d %d", &n, &m);
        for (int i = 1; i <= m; i++) {
            int x, y, z;
            scanf("%d %d %d", &x, &y, &z);
            add(x, y, z);
        }
        scanf("%d %d", &s, &t);
        dij();
        int res = 0;
        if (dis[0][t] == dis[1][t] - 1) res = ans[0][t] + ans[1][t];
        else res = ans[0][t];
        printf("%d\n", res);
    }
}

C++14(g++5.4) 解法, 执行用时: 29ms, 内存消耗: 608K, 提交时间: 2019-08-05 20:21:53

//Author:XuHt
#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 1006, INF = 0x3f3f3f3f;
int n, m, d[N][2], c[N][2];
bool v[N][2];
vector<pair<int, int> > e[N];

inline int Dijkstra(int s, int t) {
	memset(v, 0, sizeof(v));
	memset(d, 0x3f, sizeof(d));
	memset(c, 0, sizeof(c));
	d[s][0] = 0;
	c[s][0] = 1;
	for (int i = 1; i < (n << 1); i++) {
		int now = INF, x = 1, w = 0;
		for (int j = 1; j <= n; j++)
			if (!v[j][0] && d[j][0] < now)
				now = d[x=j][w=0];
			else if (!v[j][1] && d[j][1] < now)
				now = d[x=j][w=1];
		if (now == INF) break;
		v[x][w] = 1;
		for (unsigned int i = 0; i < e[x].size(); i++) {
			int y = e[x][i].first, z = e[x][i].second;
			if (d[y][0] > now + z) {
				d[y][1] = d[y][0];
				c[y][1] = c[y][0];
				d[y][0] = now + z;
				c[y][0] = c[x][w];
			} else if (d[y][0] == now + z)
				c[y][0] += c[x][w];
			else if (d[y][1] > now + z) {
				d[y][1] = now + z;
				c[y][1] = c[x][w];
			} else if (d[y][1] == now + z)
				c[y][1] += c[x][w];
		}
	}
	return c[t][0] + (d[t][1] == d[t][0] + 1 ? c[t][1] : 0);
}

inline void Sightseeing() {
	for (int i = 1; i <= n; i++) e[i].clear();
	cin >> n >> m;
	for (int i = 1; i <= m; i++) {
		int x, y, z;
		scanf("%d %d %d", &x ,&y, &z);
		e[x].push_back(make_pair(y, z));
	}
	int s, t;
	cin >> s >> t;
	cout << Dijkstra(s, t) << endl;
}

int main() {
	int T;
	cin >> T;
	while (T--) Sightseeing();
	return 0;
}

C++ 解法, 执行用时: 14ms, 内存消耗: 652K, 提交时间: 2022-04-16 13:29:47

#include<bits/stdc++.h>
using namespace std;
const int N=1e4+5;
int n,m,last[N],last2[N],tot,tot2,dis[N],l,px,ans;
struct Node{int u,v,next;}a[N],b[N];
bool vis[N];
struct Node2{
	int pos,dis;
	bool operator < (const Node2 &x) const {return dis>x.dis;}
};
void add(int u,int v,int w){
	a[++tot].u=v;
	a[tot].v=w;
	a[tot].next=last[u];
	last[u]=tot;
}

void dij(int s){
	memset(vis,false,sizeof(vis));
	memset(dis,0x3f,sizeof(dis));
	
	priority_queue<Node2> q;
	q.push({s,0});
	dis[s]=0;
	
	while(!q.empty()){
		Node2 f=q.top();
		q.pop();
		if(vis[f.pos])continue;
		vis[f.pos]=true;
		int u=f.pos;
		for(int i=last[u];i>0;i=a[i].next){
			int v=a[i].u;
			int w=a[i].v;
			if(dis[v]>dis[u]+w){
				dis[v]=dis[u]+w;
				q.push({v,dis[v]});
			}
		}
	}
}

void dfs(int x,int far){
	if(far>l+1)return;
	if(x==px){
		if(far==l||far==l+1)ans++;
		return;
	}
	for(int i=last[x];i>0;i=a[i].next){
		if(vis[a[i].u]==false){
			vis[a[i].u]=true;
			dfs(a[i].u,far+a[i].v);
			vis[a[i].u]=false;
		}
	}
}

int main(){
	int T;
	cin>>T;
	while(T--){
		tot=0;
		memset(last,-1,sizeof(last));
		scanf("%d%d",&n,&m);
		int u,v,w;
		for(int i=0;i<m;i++){
			scanf("%d%d%d",&u,&v,&w);
			add(u,v,w);
		}
		int x,y;
		scanf("%d%d",&x,&y);
		px=y;
		ans=0;
		dij(x);
		l=dis[y];
		memset(vis,false,sizeof(vis));
		dfs(x,0);
		cout<<ans<<endl;
	}
	return 0;
}

上一题