# 7-8 Path Judgment (20 points)

Given an undirected graph with N vertices and E edges, determine whether a path exists between the given two vertices. Assume that the vertices are numbered from 0 to N_1.

### Input format:

The first line of input gives two integers, N (0 < N < 10) and E, which are the vertices and edges of the graph, respectively.

Subsequently, line E gives two endpoints of an edge. Numbers in each line are separated by a space.

The last line gives two vertex numbers i, j (0 < i, j < N), separated by a space between I and j.

### Output format:

If there is a path between i and j, output "There is a path between i and j."

Otherwise, output "There is no path between i and j."

### Input Sample 1:

```7 6
0 1
2 3
1 4
0 2
1 3
5 6
0 3```

No empty line at end

### Output Sample 1:

`There is a path between 0 and 3.`

No empty line at end

```#include<bits/stdc++.h>
using namespace std;
int fa;

int find(int x)
{
if (fa[x] == x)
return x;
else
return fa[x] = find(fa[x]);
}

void merge(int x, int y)
{
int fa_x = find(x);//Find the ancestor of x
int fa_y = find(y);//Find the ancestor of y
if (fa_x != fa_y)//If x, y's ancestor is not an element
fa[fa_x] = fa_y;//The ancestor of x points to the ancestor of y
}

int main()
{
int i, j, n, m, a, b, k1, k2;
for (i = 0; i < 15; i++)
{
fa[i] = i;//Initialization, everyone's ancestors are themselves
}
cin >> n >> m;
for (i = 0; i < m; i++)
{
cin >> a >> b;
merge(a, b);
}
cin >> k1 >> k2;
if(find(k1) == find(k2))
printf("There is a path between %d and %d.", k1, k2);
else
printf("There is no path between %d and %d.", k1, k2);
return 0;
}```

# 7-9 Shortest Route (20 min)

Given an undirected graph with N vertices and E edges, the vertices are numbered from 0 to N_1. Determine if a path exists between two given vertices. If so, the shortest path length is given. Here, the shortest path from the defined vertex to itself is 0. When searching, assume that we always access adjacent points in increasing order from the smallest numbered vertex.

### Input format:

The first line of input gives two integers, N (0 < N < 10) and E, which are the vertices and edges of the graph, respectively. Subsequently, line E gives two vertices of an edge. Numbers in each line are separated by a space. The last line gives two vertex numbers i, j (0 < i, j < N), separated by a space between I and j.

### Output format:

If there is a path between i and j, output "The length of the shortest path between i and j is X." X is the shortest path length, otherwise output "There is no path between i and j."

### Input Sample 1:

```7 6
0 1
2 3
1 4
0 2
1 3
5 6
0 3```

No empty line at end

### Output Sample 1:

`The length of the shortest path between 0 and 3 is 2.`

No empty line at end

```#include<bits/stdc++.h>
using namespace std;
const int inf=0x3f3f3f3f;
int N,E;
int m;
void Floyd(){
for(int k=0;k<N;k++)
{
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
{
m[i][j]=min(m[i][j],m[i][k]+m[k][j]);
}
}
}
}
int main()
{
cin>>N>>E;
memset(m,inf,sizeof(m));
while(E--)
{
int x,y;
cin>>x>>y;
m[x][y]=m[y][x]=1;
}
Floyd();
int st,ed;
cin>>st>>ed;
if(st==ed)
cout<<"The length of the shortest path between "<<st<<" and "<<ed<<" is "<<0<<"."<<endl;
else if(m[st][ed]!=inf)
cout<<"The length of the shortest path between "<<st<<" and "<<ed<<" is "<<m[st][ed]<<"."<<endl;
else
cout<<"There is no path between "<<st<<" and "<<ed<<"."<<endl;
return 0;
}
```

# 7-10 contiguity matrix representation to create undirected graph (20 points)

An undirected graph G is created using the adjacency matrix representation, and the degrees of each vertex are output in turn.

### Input format:

In the first input line, two integers i (0 < I < 10) and j(j < 0) are given, which are the vertices and edges of graph G. Enter information about the vertices of the second behavior, each vertex being represented by only one character. Enter j rows in turn, with each row entering a vertex on which the edge depends.

### Output format:

The degrees of each vertex are output in turn, with no last space at the end of the line.

### Input sample:

```5 7
ABCDE
AB
BC
BE
CD
CE
DE```

No empty line at end

### Output sample:

`2 3 3 3 3`

No empty line at end

# 7-11 Contiguity Table Create undirected graph (20 minutes)

An undirected graph G is created using an adjacency table and the degrees of each vertex are output in turn.

### Input format:

In the first input line, two integers i (0 < I < 10) and j(j < 0) are given, which are the vertices and edges of graph G. Enter information about the vertices of the second behavior, each vertex being represented by only one character. Enter j rows in turn, with each row entering a vertex on which the edge depends.

### Output format:

The degrees of each vertex are output in turn, with no last space at the end of the line.

### Input sample:

```5 7
ABCDE
AB
BC
BE
CD
CE
DE```

No empty line at end

### Output sample:

`2 3 3 3 3`

No empty line at end

```#include <bits/stdc++.h>
using namespace std;
const int N=1e3;
int a[N][N],cnt[N];
int n,m;
int main(){
cin>>n>>m;
getchar();

map<char,int> mp;
char u,v;//Two points connected by an edge, subsequently used with mp

for(int i=0;i<n;i++)//Loop-in ABCDE
{
cin>>u;//Enter ABCDE
mp[u]=i;//mp numbering
}
getchar();

while(m--)//m-row data
{
cin>>u>>v;
getchar();
a[mp[u]][mp[v]]=1;
a[mp[v]][mp[u]]=1;//Marker 1 in undirected graph matrix
}

for(int i=0;i<n;i++)//Traversal in Matrix
{
for(int j=0;j<n;j++)
{
if(a[i][j])
cnt[i]++;//Statistics of each vertex
}
}

cout<<cnt;
for(int i=1;i<n;i++)
cout<<" "<<cnt[i];
cout<<endl;
return 0;
}
```

# 7-12 Dijkstra algorithm (template) (30 points)

Given an undirected graph with n(1 < n < 2500) points m(1 < m < 6200) edges, the shortest path from s to t is obtained.

### Input format:

The first row contains four integers n, m, s, t, separated by spaces.

The next m lines, three positive integers per line   si, ti, wi (1 < wi < 109), indicating a slave from si   reach   Ti   Count Reg   Wi   Edge.

### Output format:

An integer representing the shortest path length from s to t. The data guarantees at least one road.

### Input sample:

```7 11 5 4
2 4 2
1 4 3
7 2 2
3 4 3
5 7 5
7 3 3
6 1 1
6 3 4
2 4 3
5 6 3
7 2 1```

No empty line at end

### Output sample:

`7`

No empty line at end

### Be careful:

There may be several directly connected roads between the two vertices.

```#include<bits/stdc++.h>
using namespace std;

int n,m,s,t;
int w;
const int inf=0x3f3f3f3f;

void Floyd(){
for(int k=1;k<=n;k++)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
w[i][j]=min(w[i][j],w[i][k]+w[k][j]);
w[j][i]=w[i][j];//optimization
}
}
}
cout<<w[s][t]<<endl;//Shortest path required for direct output
}

int main()
{
cin>>n>>m>>s>>t;
memset(w,inf,sizeof(w));//Initialize all to infinity
while(m--)//Data in m rows
{
int u,v,W;
cin>>u>>v>>W;