PAT A1131 Subway Map (30point(s))

In the big cities, the subway systems always look so complex to the visitors. To give you some sense, the following figure shows the map of Beijing subway. Now you are supposed to help people with your computer skills! Given the starting position of your user, your task is to find the quickest way to his/her destination.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (≤ 100), the number of subway lines. Then N lines follow, with the i-th (i=1,⋯,N) line describes the i-th subway line in the format:

M S[1] S[2] … S[M]

where M (≤ 100) is the number of stops, and S[i]'s (i=1,⋯,M) are the indices of the stations (the indices are 4-digit numbers from 0000 to 9999) along the line. It is guaranteed that the stations are given in the correct order – that is, the train travels between S[i] and S[i+1] (i=1,⋯,M−1) without any stop.

Note: It is possible to have loops, but not self-loop (no train starts from S and stops at S without passing through another station). Each station interval belongs to a unique subway line. Although the lines may cross each other at some stations (so called “transfer stations”), no station can be the conjunction of more than 5 lines.

After the description of the subway, another positive integer K (≤ 10) is given. Then K lines follow, each gives a query from your user: the two indices as the starting station and the destination, respectively.

The following figure shows the sample map.
samplemap.jpg

Note: It is guaranteed that all the stations are reachable, and all the queries consist of legal station numbers.

Output Specification:

For each query, first print in a line the minimum number of stops. Then you are supposed to show the optimal path in a friendly format as the following:

Take Line#X1 from S1 to S2.
Take Line#X2 from S2 to S3.
......

where Xi’s are the line numbers and Si’s are the station indices. Note: Besides the starting and ending stations, only the transfer stations shall be printed.

If the quickest path is not unique, output the one with the minimum number of transfers, which is guaranteed to be unique.

Sample Input:

4
7 1001 3212 1003 1204 1005 1306 7797
9 9988 2333 1204 2006 2005 2004 2003 2302 2001
13 3011 3812 3013 3001 1306 3003 2333 3066 3212 3008 2302 3010 3011
4 6666 8432 4011 1306
3
3011 3013
6666 2001
2004 3001

Sample Output:

2
Take Line#3 from 3011 to 3013.
10
Take Line#4 from 6666 to 1306.
Take Line#3 from 1306 to 2302.
Take Line#2 from 2302 to 2001.
6
Take Line#2 from 2004 to 1204.
Take Line#1 from 1204 to 1306.
Take Line#3 from 1306 to 3001.
  • 思路 1:Dijkstra + DFS
    最后一个点会超时!直接堆优化后可通过,剪枝的方案还没考虑…

  • code:

最后一个点 TLE 的 Dijkstra:

void Dijkstra(int start, int destination){
	fill(d, d + maxn, INF);
	memset(vis, false, sizeof(vis));
	d[start] = 0;
	for(int i = 0; i < stops.size(); ++i){
		int now = -1, Min = INF;
		for(int j = 0; j < stops.size(); ++j){
			int tmps = stops[j];
			if(vis[tmps] == false && d[tmps] < Min){
				Min = d[tmps];
				now = tmps;
			}
		}
		if(now == -1 || now == destination) return;
		vis[now] = true;
		for(int j = 0; j < G[now].size(); ++j){
			int nex = G[now][j];
			if(vis[nex] == false){
				if(d[now] + 1 < d[nex]){
					d[nex] = d[now] + 1;
					pre[nex].clear();
					pre[nex].push_back(now);
				}else if(d[now] + 1 == d[nex]){
					pre[nex].push_back(now);
				}
			} 
		}
	}
}
  • 关于堆优化:减少的是每轮中找Min(当前d[]最小的点)的时间开销

使用结构体记录点的id 和 d(到源点的最短距离),并重载<使按d排序,构造优先队列priority_queue<node> pq;

step 1:最开始将node{id = 源点,d = 0} pushpq
step 2:进入循环后(while(!pq.empty())), 每次将当前最优点出队,并以该点为跳点更新所有没访问过的点 p2,并将更新的点入队,【注意】pq中存放的是副本,外部的修改对pq内元素没有影响。所以,还是需要一个数组d[]来记录最优结果。同时,对同一个点可能被多次优化push进了pq,也就是pq中存在重复idnode。但是,由于队首始终是最优的那个(最优的总是先出队),所以第二次访问到相同id的节点 vis[id] == true; 要跳过continue; p1

  • 坑点:
  1. Wrong 1:样例 1 3 不能直接用has来找中转站,环的头尾重合has[start]=2;
    开始我用每个stop出现的次数来识别是否是中转站,if has[i]>1:true;,这样不行,环型线路首尾重合,如has[3011] == 2

  2. Wrong 2:%04d :样例4
    老坑了,n位编号,要对齐输出

堆优化后的Dijkstra:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 10010, INF = 0x3fffffff;
vector<int> G[maxn], stops;
int d[maxn], has[maxn], line[maxn][maxn];
bool vis[maxn];
struct node{
	int id, d;
	node(int _id, int _d) : id(_id), d(_d) {}
	bool operator < (const node & a) const {	//成员函数前的const是为了防止对this的修改
		return d > a.d;
	} 
};
vector<int> pre[maxn];
void Dijkstra(int start, int destination){
	fill(d, d + maxn, INF);
	memset(vis, false, sizeof(vis));
	d[start] = 0;
	priority_queue<node> q;
	q.push(node(start, 0));
	while(!q.empty()){
		node now = q.top();
		q.pop();
		if(now.id == destination) return;
		if(vis[now.id]) continue;	//p1 过滤掉编号重复的点!
		vis[now.id] = true;
		for(int i = 0; i < G[now.id].size(); ++i){
			int nex = G[now.id][i];
			if(vis[nex] == false){	// p2
				if(now.d + 1 < d[nex]){
					d[nex] = now.d + 1;
					pre[nex].clear();
					pre[nex].push_back(now.id);
					q.push(node(nex, d[nex]));
				}else if(now.d + 1 == d[nex]){
					pre[nex].push_back(now.id);					
					q.push(node(nex, d[nex]));
				}
			}
		}
	}
}
int min_tranfer;
vector<int> tmp, ans;
void DFS(int id, int start){
	if(id == start){
		tmp.push_back(id);
		int cnt_transfer = 0;
		for(int i = tmp.size() - 2; i > 0; --i){
			int pre_stop = tmp[i+1], now_stop = tmp[i], nex_stop = tmp[i-1];
			if(line[pre_stop][now_stop] != line[now_stop][nex_stop]){	//now_stops是中转站 
				cnt_transfer++;		//Wrong 1:样例 1 3 不能直接用has来找中转站,环的头尾重合has[start]=2; 
			}
		}	
		if(cnt_transfer < min_tranfer){
			min_tranfer = cnt_transfer;
			ans = tmp;
		} 
		tmp.pop_back();
		return;
	}
	for(int i = 0; i < pre[id].size(); ++i){
		int nex = pre[id][i];
		tmp.push_back(id);
		DFS(nex, start);
		tmp.pop_back();
	}
}
void Print(){
	int i = ans.size() - 1, j = i - 1, len = ans.size() - 1, pre_line;
	printf("%d\n", ans.size() - 1);
	while(j >= 0){
		pre_line = line[ans[i]][ans[j]];
		while(j > 0 && line[ans[j]][ans[j - 1]] == pre_line) j--;
		printf("Take Line#%d from %04d to %04d.\n", pre_line, ans[i], ans[j]);	//Wrong 2:%04d :样例4 
		i = j--;
	}
}
int main(){
	int n;
	scanf("%d", &n);
	for(int i = 1; i <= n; ++i){
		int ns, tmps, pres;
		scanf("%d", &ns);
		for(int j = 0; j < ns; ++j){
			scanf("%d", &tmps);
			has[tmps]++;
			if(has[tmps] == 1) stops.push_back(tmps);
			if(j != 0){
				line[pres][tmps] = line[tmps][pres] = i;
				G[pres].push_back(tmps);
				G[tmps].push_back(pres);
			} 
			pres = tmps;
		}
	}
	int nq;
	scanf("%d", &nq);
	for(int i = 0; i < nq; ++i){
		int ts, td;
		scanf("%d %d", &ts, &td);
		Dijkstra(ts, td);
		min_tranfer = INF;
		DFS(td, ts);
		Print();
		ans.clear();
	}
	return 0;
} 

在这里插入图片描述

  • T3 code:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 10010, INF = 0x3fffffff;
int line[maxn][maxn], d[maxn];
vector<int> G[maxn];
bool vis[maxn];

struct node
{
    int id, d;
    bool operator < (const node & tmp) const { return d > tmp.d; }
};
vector<int> pre[maxn];
void Dijkstra(int s, int dest)
{
    fill(d, d+maxn, INF);
    memset(vis, false, sizeof(vis));
    d[s] = 0;
    priority_queue<node> pq;
    pq.push(node{s, 0});
    while(!pq.empty())
    {
        node now = pq.top();
        pq.pop();
        if(now.id == dest) return;
        if(vis[now.id]) continue;
        vis[now.id] = true;
        for(int i = 0; i < G[now.id].size(); ++i)
        {
            int nex = G[now.id][i];
            if(vis[nex] == false)
            {
                if(now.d + 1 < d[nex])
                {
                    d[nex] = now.d + 1;
                    pre[nex].clear();
                    pre[nex].push_back(now.id);
                    pq.push(node{nex, d[nex]});
                }else if(now.d + 1 == d[nex])
                {
                    pre[nex].push_back(now.id);
                    pq.push(node{nex, d[nex]});
                }
            }
        }
    }
}
bool isTran(vector<int> & v, int i)
{
    int pre = v[i+1], now = v[i], nex = v[i-1];
    return (line[pre][now] != line[now][nex]) ? true : false;
}

vector<int> tmp, ans;
void DFS(int id, int s, int & min_tran)
{
    tmp.push_back(id);
    if(id == s)
    {
        int cnt_tran = 0;
        for(int i = tmp.size()-2; i > 0; --i)
        {
             if(isTran(tmp, i)) cnt_tran++;
        }
        if(cnt_tran < min_tran)
        {
            min_tran = cnt_tran;
            ans = tmp;
        }
        tmp.pop_back();
        return;
    }
    for(int i = 0; i < pre[id].size(); ++i)
    {
        int nex = pre[id][i];
        DFS(nex, s, min_tran);
    }
    tmp.pop_back();
}
int main()
{
    int n, m;
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i)
    {
        scanf("%d", &m);
        int tmp, pre;
        for(int j = 0; j < m; ++j)
        {
            scanf("%d", &tmp);
            if(j != 0)
            {
                line[pre][tmp] = line[tmp][pre] = i;
                G[tmp].push_back(pre);
                G[pre].push_back(tmp);
            }
            pre = tmp;
        }
    }
    scanf("%d", &n);
    for(int i = 0; i < n; ++i)
    {
        int s, e, Min = INF;
        scanf("%d %d", &s, &e);
        Dijkstra(s, e);
        DFS(e, s, Min);
        printf("%d\n", d[e]);
        int last = s, nex;
        for(int i = ans.size()-2; i > 0; --i)
        {
            if(isTran(ans, i))
            {
                nex = ans[i];
                printf("Take Line#%d from %04d to %04d.\n", line[ans[i+1]][nex], last, nex);
                last = nex;
            }
        }
        printf("Take Line#%d from %04d to %04d.\n", line[ans[1]][e], last, e);
        ans.clear();
    }
    return 0;
}

  • 思路 2:直接DFS,找到路径后不断优化取最优

  • 坑点:

  1. DFS过程中要记录访问过的节点(vis[id] = true;),防止回头。但是,从这一节点出发所有路线都跑完回溯以后,要将这一节点“开锁(vis[id] = false;)”,因为从其他路线可能也要经过这个点(cross)

  2. Wrong 1:两个标尺问题,更新第一个标尺的同时,千万不要忘了顺带更新第二个标尺,否则第一个标尺相同时,无法在第一个标尺基础上更新第二个标尺

  • code:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 10000, INF = 0x3fffffff;
vector<int> G[maxn];
int line[maxn][maxn];
bool vis[maxn];

vector<int> tmp, ans;

void DFS(int id, int destination, int pre, int cnt_transfer, int & min_stop, int & min_tran){   
	if(tmp.size() > min_stop) return;
	if(id == destination){
		tmp.push_back(id);
//		int cnt_transfer = 0;	//最找到路径计算一次,重复计算量大 
//		for(int i = tmp.size() - 2; i > 0; --i){
//			int pre = tmp[i+1], now = tmp[i], nex = tmp[i-1];
//			if(line[pre][now] != line[now][nex]){
//				cnt_transfer++;
//			}
//		}
		if(tmp.size() < min_stop){
			min_stop = tmp.size();
			//Wrong 1:样例 0 3 :这里也要更新第二标尺:中转站数量;
			min_tran = cnt_transfer;
			ans = tmp;	
		}else if(tmp.size() == min_stop && cnt_transfer < min_tran){
			min_tran = cnt_transfer;
			ans = tmp;
		}
		tmp.pop_back();
		return;
	}
	vis[id] = true;
	for(int i = 0; i < G[id].size(); ++i){
		int nex = G[id][i];
		if(vis[nex] == false){
//			int pre = tmp.size() > 0 ? tmp.back() : 0;
			tmp.push_back(id);
			if(tmp.size() > 0 && line[pre][id] != line[id][nex]){
				DFS(nex, destination, id, cnt_transfer + 1, min_stop, min_tran);	
			}else{
				DFS(nex, destination, id, cnt_transfer, min_stop, min_tran);	
			}
			tmp.pop_back();
		}
	}
	vis[id] = false;
}
void Print(){
	int i = 0, j = i + 1, pre_line;
	printf("%d\n", ans.size() - 1);
	while(j < ans.size()){
		pre_line = line[ans[i]][ans[j]];
		while(j < ans.size() - 1 && line[ans[j]][ans[j + 1]] == pre_line) j++;
		printf("Take Line#%d from %04d to %04d.\n", pre_line, ans[i], ans[j]);	//04d :样例4 
		i = j++;
	}
}
int main(){
	int n, nq;
	scanf("%d", &n);
	for(int i = 1; i <= n; ++i){
		int ns, tmps, pres;
		scanf("%d", &ns);
		for(int j = 0; j < ns; ++j){
			scanf("%d", &tmps);
			if(j != 0){
				line[pres][tmps] = line[tmps][pres] = i;
				G[pres].push_back(tmps);
				G[tmps].push_back(pres);
			}
			pres = tmps;
		}
	}
	scanf("%d", &nq);
	for(int i = 0; i < nq; ++i){
		int ts, td, min_stop = INF, min_tran = INF;
		scanf("%d %d", &ts, &td);
		DFS(ts, td, ts, 0, min_stop, min_tran);
		Print();
		ans.clear();
	}
	return 0;
} 

在这里插入图片描述

  • T3 code:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 10010, INF = 0x3fffffff;
int line[maxn][maxn];
vector<int> G[maxn];
bool vis[maxn];

bool isTran(vector<int> & v, int i)
{
    int pre = v[i+1], now = v[i], nex = v[i-1];
    return (line[pre][now] != line[now][nex]) ? true : false;
}

vector<int> tmp, ans;
void DFS(int id, int s, int & min_tran, int & min_spot)
{
    tmp.push_back(id);
    if(id == s)
    {
        int cnt_tran = 0;
        for(int i = tmp.size()-2; i > 0; --i)
        {
             if(isTran(tmp, i)) cnt_tran++;
        }
        if(tmp.size() < min_spot)
        {
            min_spot = tmp.size();
            min_tran = cnt_tran;
            ans = tmp;
        }else if(tmp.size() == min_spot)
        {
            if(cnt_tran < min_tran)
            {
                min_tran = cnt_tran;
                ans = tmp;
            }
        }
        tmp.pop_back();
        return;
    }
    vis[id] = true;
    for(int i = 0; i < G[id].size(); ++i)
    {
        int nex = G[id][i];
        if(vis[nex] == false)
            DFS(nex, s, min_tran, min_spot);
    }
    vis[id] = false;
    tmp.pop_back();
}
int main()
{
    int n, m;
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i)
    {
        scanf("%d", &m);
        int tmp, pre;
        for(int j = 0; j < m; ++j)
        {
            scanf("%d", &tmp);
            if(j != 0)
            {
                line[pre][tmp] = line[tmp][pre] = i;
                G[tmp].push_back(pre);
                G[pre].push_back(tmp);
            }
            pre = tmp;
        }
    }
    scanf("%d", &n);
    for(int i = 0; i < n; ++i)
    {
        int s, e, MinTran = INF, MinSpot = INF;
        scanf("%d %d", &s, &e);
        memset(vis, false ,sizeof(vis));
        DFS(e, s, MinTran, MinSpot);
        printf("%d\n", ans.size()-1);
        int last = s, nex;
        for(int i = ans.size()-2; i > 0; --i)
        {
            if(isTran(ans, i))
            {
                nex = ans[i];
                printf("Take Line#%d from %04d to %04d.\n", line[ans[i+1]][nex], last, nex);
                last = nex;
            }
        }
        printf("Take Line#%d from %04d to %04d.\n", line[ans[1]][e], last, e);
        ans.clear();
    }
    return 0;
}

  • T4 code:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 10010, INF = 0x3fffffff;
int Line[maxn][maxn], d[maxn];
bool vis[maxn];
vector<int> G[maxn];
struct node
{
    int id, d;
    bool operator < (const node & tmp) const { return d > tmp.d; }
};
vector<int> pre[maxn];
void Dijkstra(int start, int dest)
{
    fill(d, d + maxn, INF);
    memset(vis, false, sizeof(vis));
    d[start] = 0;
    priority_queue<node> pq;
    pq.push(node{start, 0});
    while(!pq.empty())
    {
        node now = pq.top();
        pq.pop();
        if(now.id == dest) return;
        if(vis[now.id]) continue;
        vis[now.id] = true;
        for(int i = 0; i < G[now.id].size(); ++i)
        {
            int nex = G[now.id][i];
            if(vis[nex] == false)
            {
                if(now.d + 1 < d[nex])
                {
                    d[nex] = now.d + 1;
                    pre[nex].clear();
                    pre[nex].push_back(now.id);
                    pq.push(node{nex, d[nex]});
                }else if(now.d + 1 == d[nex])
                {
                    pre[nex].push_back(now.id);
                    pq.push(node{nex, d[nex]});
                }
            }
        }
    }
}
bool isTransfer(int pre, int now, int nex)
{
    if(Line[pre][now] != Line[now][nex]) return true;
    else return false;
}
vector<int> tmp, ans;
void DFS(int id, int s, int cnt, int & min_tran, int p)
{
    tmp.push_back(id);
    if(id == s)
    {
        if(cnt < min_tran)
        {
            min_tran = cnt;
            ans = tmp;
        }
        tmp.pop_back();
        return;
    }
    for(int nex : pre[id])
    {
//        if(id != p && isTransfer(p, id, nex)) cnt++;
        if(id != p && isTransfer(p, id, nex)) 
            DFS(nex, s, cnt + 1, min_tran, id);
        else 
            DFS(nex, s, cnt, min_tran, id);
            
    }
    tmp.pop_back();
}
int main()
{
    int n;
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i)
    {
        int k, pre;
        scanf("%d", &k);
        for(int j = 0; j < k; ++j)
        {
            int tmp;
            scanf("%d", &tmp);
            if(j)
            {
                Line[pre][tmp] = Line[tmp][pre] = i;
                G[tmp].push_back(pre);
                G[pre].push_back(tmp);
            }
            pre = tmp;
        }
    }
    int nq;
    scanf("%d", &nq);
    for(int i = 0; i < nq; ++i)
    {
        int q1, q2;
        scanf("%d %d", &q1, &q2);
        Dijkstra(q1, q2);
        int min_tran = INF;
        DFS(q2, q1, 0, min_tran, q2);
        printf("%d\n", ans.size()-1);
        int r = ans.size()-1, l = r-1;
        while(r > 0)
        {
            while(l > 0 && !isTransfer(ans[l+1], ans[l], ans[l-1])) l--;
            printf("Take Line#%d from %04d to %04d.\n", Line[ans[l+1]][ans[l]], ans[r], ans[l]);
            r = l--;
        }
        ans.clear();
    }
    return 0;
}

  • BF: 最后一个点超时
#include <bits/stdc++.h>
using namespace std;
const int maxn = 10010, INF = 0x3fffffff;
int Line[maxn][maxn], d[maxn], idex = 0;
vector<int> G[maxn];

unordered_set<int> pre[maxn], v;
void BF(int start)
{
    fill(d, d + maxn, INF);
    d[start] = 0;
    for(int i = 0; i < v.size()-1; ++i)
    {
        bool isUpdate = false;
        for(int now : v)
        {
            for(int j = 0; j < G[now].size(); ++j)
            {
                int nex = G[now][j];
                if(d[now] + 1 < d[nex])
                {
                    d[nex] = d[now] + 1;
                    pre[nex].clear();
                    pre[nex].insert(now);
                    isUpdate = true;
                }else if(d[now] + 1 == d[nex])
                {
                    pre[nex].insert(now);
                    isUpdate = true;
                }
            }
        }
        if(!isUpdate) return;
    }
}

bool isTransfer(int pre, int now, int nex)
{
    if(Line[pre][now] != Line[now][nex]) return true;
    else return false;
}
vector<int> tmp, ans;
void DFS(int id, int s, int cnt, int & min_tran, int p)
{
    tmp.push_back(id);
    if(id == s)
    {
        if(cnt < min_tran)
        {
            min_tran = cnt;
            ans = tmp;
        }
        tmp.pop_back();
        return;
    }
    for(int nex : pre[id])
    {
//        if(id != p && isTransfer(p, id, nex)) cnt++;
        if(id != p && isTransfer(p, id, nex))
            DFS(nex, s, cnt + 1, min_tran, id);
        else
            DFS(nex, s, cnt, min_tran, id);

    }
    tmp.pop_back();
}
int main()
{
    int n;
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i)
    {
        int k, pre;
        scanf("%d", &k);
        for(int j = 0; j < k; ++j)
        {
            int tmp;
            scanf("%d", &tmp);
            v.insert(tmp);
            if(j)
            {
                Line[pre][tmp] = Line[tmp][pre] = i;
                G[tmp].push_back(pre);
                G[pre].push_back(tmp);
            }
            pre = tmp;
        }
    }
    int nq;
    scanf("%d", &nq);
    for(int i = 0; i < nq; ++i)
    {
        int q1, q2;
        scanf("%d %d", &q1, &q2);
        BF(q1);
        int min_tran = INF;
        DFS(q2, q1, 0, min_tran, q2);
        printf("%d\n", ans.size()-1);
        int r = ans.size()-1, l = r-1;
        while(r > 0)
        {
            while(l > 0 && !isTransfer(ans[l+1], ans[l], ans[l-1])) l--;
            printf("Take Line#%d from %04d to %04d.\n", Line[ans[l+1]][ans[l]], ans[r], ans[l]);
            r = l--;
        }
        ans.clear();
    }
    return 0;
}

  • SPFA
#include <bits/stdc++.h>
using namespace std;
const int maxn = 10010, INF = 0x3fffffff;
int Line[maxn][maxn], d[maxn];
vector<int> G[maxn];
unordered_set<int> pre[maxn], v;

bool inq[maxn];
int cnt_inq[maxn];
void SPFA(int start)
{
    fill(d, d + maxn, INF);
    memset(cnt_inq, 0, sizeof(cnt_inq));
    d[start] = 0;
    queue<int> q;
    q.push(start);
    inq[start] = true;
    cnt_inq[start]++;
    while(!q.empty())
    {
        int now = q.front();
        q.pop();
        inq[now] = false;
        for(int i = 0; i < G[now].size(); ++i)
        {
            int nex = G[now][i];
            if(d[now] + 1 < d[nex])
            {
                d[nex] = d[now] + 1;
                pre[nex].clear();
                pre[nex].insert(now);
                if(inq[nex] == false)
                {
                    q.push(nex);
                    inq[nex] = true;
                    cnt_inq[nex]++;
                    if(cnt_inq[nex] >= v.size()) return;
                }
            }else if(d[now] + 1 == d[nex])
            {
                pre[nex].insert(now);
                if(inq[nex] == false)
                {
                    q.push(nex);
                    inq[nex] = true;
                    cnt_inq[nex]++;
                    if(cnt_inq[nex] >= v.size()) return;
                }
            }
        }
    }
}
bool isTransfer(int pre, int now, int nex)
{
    if(Line[pre][now] != Line[now][nex]) return true;
    else return false;
}
vector<int> tmp, ans;
void DFS(int id, int s, int cnt, int & min_tran, int p)
{
    tmp.push_back(id);
    if(id == s)
    {
        if(cnt < min_tran)
        {
            min_tran = cnt;
            ans = tmp;
        }
        tmp.pop_back();
        return;
    }
    for(int nex : pre[id])
    {
//        if(id != p && isTransfer(p, id, nex)) cnt++;
        if(id != p && isTransfer(p, id, nex))
            DFS(nex, s, cnt + 1, min_tran, id);
        else
            DFS(nex, s, cnt, min_tran, id);

    }
    tmp.pop_back();
}
int main()
{
    int n;
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i)
    {
        int k, pre;
        scanf("%d", &k);
        for(int j = 0; j < k; ++j)
        {
            int tmp;
            scanf("%d", &tmp);
            v.insert(tmp);
            if(j)
            {
                Line[pre][tmp] = Line[tmp][pre] = i;
                G[tmp].push_back(pre);
                G[pre].push_back(tmp);
            }
            pre = tmp;
        }
    }
    int nq;
    scanf("%d", &nq);
    for(int i = 0; i < nq; ++i)
    {
        int q1, q2;
        scanf("%d %d", &q1, &q2);
        SPFA(q1);
        int min_tran = INF;
        DFS(q2, q1, 0, min_tran, q2);
        printf("%d\n", ans.size()-1);
        int r = ans.size()-1, l = r-1;
        while(r > 0)
        {
            while(l > 0 && !isTransfer(ans[l+1], ans[l], ans[l-1])) l--;
            printf("Take Line#%d from %04d to %04d.\n", Line[ans[l+1]][ans[l]], ans[r], ans[l]);
            r = l--;
        }
        ans.clear();
    }
    return 0;
}
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
应用背景为变电站电力巡检,基于YOLO v4算法模型对常见电力巡检目标进行检测,并充分利用Ascend310提供的DVPP等硬件支持能力来完成流媒体的传输、处理等任务,并对系统性能做出一定的优化。.zip深度学习是机器学习的一个子领域,它基于人工神经网络的研究,特别是利用多层次的神经网络来进行学习和模式识别。深度学习模型能够学习数据的高层次特征,这些特征对于图像和语音识别、自然语言处理、医学图像分析等应用至关重要。以下是深度学习的一些关键概念和组成部分: 1. **神经网络(Neural Networks)**:深度学习的基础是人工神经网络,它是由多个层组成的网络结构,包括输入层、隐藏层和输出层。每个层由多个神经元组成,神经元之间通过权重连接。 2. **前馈神经网络(Feedforward Neural Networks)**:这是最常见的神经网络类型,信息从输入层流向隐藏层,最终到达输出层。 3. **卷积神经网络(Convolutional Neural Networks, CNNs)**:这种网络特别适合处理具有网格结构的数据,如图像。它们使用卷积层来提取图像的特征。 4. **循环神经网络(Recurrent Neural Networks, RNNs)**:这种网络能够处理序列数据,如时间序列或自然语言,因为它们具有记忆功能,能够捕捉数据中的时间依赖性。 5. **长短期记忆网络(Long Short-Term Memory, LSTM)**:LSTM 是一种特殊的 RNN,它能够学习长期依赖关系,非常适合复杂的序列预测任务。 6. **生成对抗网络(Generative Adversarial Networks, GANs)**:由两个网络组成,一个生成器和一个判别器,它们相互竞争,生成器生成数据,判别器评估数据的真实性。 7. **深度学习框架**:如 TensorFlow、Keras、PyTorch 等,这些框架提供了构建、训练和部署深度学习模型的工具和库。 8. **激活函数(Activation Functions)**:如 ReLU、Sigmoid、Tanh 等,它们在神经网络中用于添加非线性,使得网络能够学习复杂的函数。 9. **损失函数(Loss Functions)**:用于评估模型的预测与真实值之间的差异,常见的损失函数包括均方误差(MSE)、交叉熵(Cross-Entropy)等。 10. **优化算法(Optimization Algorithms)**:如梯度下降(Gradient Descent)、随机梯度下降(SGD)、Adam 等,用于更新网络权重,以最小化损失函数。 11. **正则化(Regularization)**:技术如 Dropout、L1/L2 正则化等,用于防止模型过拟合。 12. **迁移学习(Transfer Learning)**:利用在一个任务上训练好的模型来提高另一个相关任务的性能。 深度学习在许多领域都取得了显著的成就,但它也面临着一些挑战,如对大量数据的依赖、模型的解释性差、计算资源消耗大等。研究人员正在不断探索新的方法来解决这些问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值