/*
#include <iostream>
#include<cstring>
using namespace std;
typedef struct arcnode
{
int weight;
int adjvex;
arcnode *nextarc;
}arcnode;
typedef struct vnode
{
int data;
arcnode *firstarc;
}Adjvex[20];
struct Graph
{
int vexnum, arcnum;
Adjvex vertices;
};
bool vis[100];
int LocateVex(Graph G,int u)
{
for(int i=0; i<G.vexnum; i++)
{
if(G.vertices[i].data == u)
return i;
}
return 0;
}
void create_graph(Graph &G)
{
cin >> G.vexnum >> G.arcnum;
int u, v;
for(int i=0; i<G.vexnum; i++)
{
G.vertices[i].data = i+1;
G.vertices[i].firstarc = NULL;
}
arcnode *p, *q;
for(int i=0; i<G.arcnum; i++)
{
cin >> u >> v;
p = new arcnode;
q = new arcnode;
if(p == NULL || q == NULL)
{
return;
}
int k = LocateVex(G, u);
int kk = LocateVex(G, v);
p->adjvex = kk;
p->nextarc = G.vertices[k].firstarc;
G.vertices[k].firstarc = p;
q->adjvex = k;
q->nextarc = G.vertices[kk].firstarc;
G.vertices[kk].firstarc = q;
}
}
void dfs(Graph G, int u)
{
cout << u << " ";
vis[u] = true;
int k = LocateVex(G, u);
arcnode * p = G.vertices[k].firstarc;
if(p)
{
k = p->adjvex;
int w = G.vertices[k].data;
if(!vis[w])
dfs(G, w);
p = p->nextarc;/// 控制返回到上一层 的再往下走
}
}
int main()
{
Graph G;
memset(vis, false, sizeof(vis));
create_graph(G);
dfs(G, G.vertices[0].data);
return 0;
}
*/
/*
8 9
1 2
1 3
3 6
3 7
2 5
2 4
4 8
5 8
6 7
1 3 7 6 2 4 8 5
Process returned 0 (0x0) execution time : 47.968 s
Press any key to continue.
*/
/*
#include<iostream>
#include<cstring>
using namespace std;
int map1[100][100];
bool vis[100];
int n, m;
void dfs(int u)
{
cout << u << " ";
vis[u] = true;
for(int i=1; i<=n; i++)
{
if(map1[u][i] && !vis[i])
dfs(i);
}
}
int main()
{
memset(vis, false, sizeof(vis));
memset(map1, 0, sizeof(map1));
cin >> n >> m;
int u, v;
for(int i=0; i<m; i++)
{
cin >> u >> v;
map1[u][v] = 1;
}
dfs(1);///编号从1开始
return 0;
}
*/
/*
8 9
1 2
1 3
3 6
3 7
2 5
2 4
4 8
5 8
6 7
1 2 4 8 5 3 6 7
Process returned 0 (0x0) execution time : 6.238 s
Press any key to continue.
*/
/*
/// BFS
#include<iostream>
#include<cstring>
using namespace std;
struct arcnode
{
int adjvex;
arcnode *nextarc;
};
struct vnode
{
int data;
arcnode *firstarc;
};
struct Graph
{
int vexnum, arcnum;
vnode vertices[20];
};
int q[100];
int head, tail;
bool vis[100];
int LocateVex(Graph G, int u)
{
for(int i=0; i<G.vexnum; i++)
{
if(G.vertices[i].data == u)
return i;
}
return 0;
}/// G.vertices[i].data 存的可是起点的编号!!!
void create_graph(Graph &G)
{
cin >> G.vexnum >> G.arcnum;
int u, v;
for(int i=0; i<G.vexnum; i++)
{
G.vertices[i].data = i+1;
G.vertices[i].firstarc = NULL;
}
arcnode *p, *q;
for(int i=0; i<G.arcnum; i++)
{
cin >> u >> v;
p = new arcnode;
q = new arcnode;
if(p == NULL || q == NULL)
{
return;
}
int k = LocateVex(G, u);
int kk = LocateVex(G, v);
p->adjvex = kk;
p->nextarc = G.vertices[k].firstarc;
G.vertices[k].firstarc = p;
q->adjvex = k;
q->nextarc = G.vertices[kk].firstarc;
G.vertices[kk].firstarc = q;
}
}
void BFS(Graph G, int u)
{
head = 0;
tail = 0;
q[tail++] = u;
vis[u] = true;
int ii=0;
while(head!=tail)///转圈哭啊 由于没设临时变量u 导致head变了 q[head]变了 整个结果都废了 卡了我好久啊
{ ///真的开始怀疑人生啦 还好最后关头对了 T^T
int u = q[head];
cout << u << " ";
head++;
int k = LocateVex(G, u);///找到了以3为起点的边的编号
for(arcnode *p=G.vertices[k].firstarc; p!=NULL; p = p->nextarc)
{
int kk = p->adjvex;
int w = G.vertices[kk].data;
if(!vis[w])
{
q[tail++] = w;
vis[w] = true;
}
}
}
}
int main()
{
memset(vis, false, sizeof(vis));
Graph G;
create_graph(G);
BFS(G, G.vertices[0].data);
return 0;
}
*/
/*
8 9
1 2
1 3
3 6
3 7
2 5
2 4
4 8
5 8
6 7
1 3 2 7 6 4 5 8
Process returned 0 (0x0) execution time : 3.153 s
Press any key to continue.
*/
/*
#include<iostream>
#include<cstring>
using namespace std;
const int inf = 65535;
int map1[100][100];
int path[100];
bool vis[100];
int d[100];
int n, m;
void dijkstra(int s)
{
for(int i=0; i<n; i++)/// the distance of s~i
{
if(i == s)
continue;
d[i] = map1[s][i]; /// w, inf
if(d[i]<inf)
path[i] = s;
}
int v;
for(int i=0; i<n-1; i++)
{
int min1 = inf;
for(int j=0; j<n; j++)
{
if(!vis[j] && d[j]<min1)
{
v = j;
min1 = d[j];
}
}
vis[v] = true;
for(int j=0; j<n; j++)
{
if(!vis[j] && d[j] > d[v]+map1[v][j])
{
d[j] = d[v]+map1[v][j];
path[j] = v;
}
}
}
int k = 3;
cout << k << ' ';
while(path[k]!=-1)
{
cout << path[k] << ' ';
k = path[k];
}
cout << d[3] << endl;/// 0 2 1 3
}
int main()
{
cin >> n >> m;
memset(vis, false, sizeof(vis));
memset(path, -1, sizeof(path));
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
if(i == j)
map1[i][j] = 0;
else
map1[i][j] = inf;
}
}
int u, v, w;
for(int i=0; i<m; i++)
{
cin >>u >> v >> w;
map1[u][v] = w;
map1[v][u] = w;
}
int s;
cin >> s;
vis[s] = true;
d[s] = 0;
dijkstra(s);
return 0;
}
*/
/*
4 5
0 1 3
1 3 1
1 2 1
0 2 1
2 3 3
1
*/
/*
3
*/
图
最新推荐文章于 2022-06-06 09:43:01 发布