实验报告:实验5-图
集美大学课程实验报告-4-图
项目名称 | 内容 |
---|---|
课程名称 | 数据结构 |
班级 | 网安2411 |
指导教师 | 郑如滨 |
学生姓名 | 李斌财 |
学号 | 202421336021 |
实验项目名称 | 图 |
上机实践日期 | |
上机实践时间 | 2学时 |
一、目的
图的基本运用
prime,dijstra,kruskal算法
二、实验内容与设计思想
pta 图着色问题
code
#include<iostream>
#include<vector>
#include<unordered_set>
using namespace std;
int main() {
int V, E, K;
cin >> V >> E >> K;
// 创建邻接表
vector<vector<int>> adj(V+1);
for(int i=0; i<E; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
int N;
cin >> N;
while(N--) {
vector<int> color(V+1);
unordered_set<int> colorSet;
// 读取颜色并统计颜色种类
for(int i=1; i<=V; i++) {
cin >> color[i];
colorSet.insert(color[i]);
}
// 检查颜色数量是否为K
if(colorSet.size() != K) {
cout << "No" << endl;
continue;
}
// 检查相邻节点颜色是否相同
bool valid = true;
for(int u=1; u<=V && valid; u++) {
for(int v : adj[u]) {
if(color[u] == color[v]) {
valid = false;
break;
}
}
}
cout << (valid ? "Yes" : "No") << endl;
}
return 0;
}
总时间复杂度:O(E + N V + N V^2) = O(N*V^2)
总空间复杂度:O(V+E + V + K) = O(V+E)
PTA:公路村村通(最小生成树)
#include<iostream>
#include<vector>
#include<climits>
using namespace std;
int main(){
int n, m;
cin >> n >> m;
// 初始化邻接矩阵
vector<vector<int>> graph(n+1, vector<int>(n+1, INT_MAX));
for(int i=0; i<m; i++){
int a, b, c;
cin >> a >> b >> c;
graph[a][b] = graph[b][a] = c;
}
vector<int> dist(n+1, INT_MAX); // 存储各节点到生成树的最小距离
vector<bool> visited(n+1, false); // 标记是否已加入生成树
dist[1] = 0; // 从节点1开始
int total = 0;
for(int i=1; i<=n; i++){
// 找到未访问节点中距离最小的
int u = -1, min_dist = INT_MAX;
for(int j=1; j<=n; j++){
if(!visited[j] && dist[j] < min_dist){
min_dist = dist[j];
u = j;
}
}
if(u == -1){ // 无法连通所有节点
cout << -1;
return 0;
}
visited[u] = true;
total += dist[u];
// 更新相邻节点距离
for(int v=1; v<=n; v++){
if(!visited[v] && graph[u][v] < dist[v]){
dist[v] = graph[u][v];
}
}
}
cout << total;
return 0;
}
时间复杂度:O(n²)
空间复杂度:O(n²)
选做部分 旅游规划(用dijkstra算法求最短路径)
#include<iostream>
#include<vector>
#include<climits>
using namespace std;
struct Road {
int length;
int cost;
};
int main() {
int n, m, s, d;
cin >> n >> m >> s >> d;
//初始化零阶矩阵;
vector<vector<Road>> graph(n,vector<Road>(n,{INT_MAX,INT_MAX}));
for(int i=0;i<n;i++){
graph[i][i]={0,0};
}
for(int i=0;i<m;i++){
int a,b,len,cos;
cin>>a>>b>>len>>cos;
graph[a][b]=graph[b][a]={len,cos};
}
vector<int> dist(n,INT_MAX);//最短距离
vector<int> totalcost(n,INT_MAX);
vector<bool> visited(n,0);
dist[s]=0;
totalcost[s]=0;
while(true){
int u=-1;
for(int i=0;i<n;i++){
if(!visited[i]&&(u==-1||dist[i]<dist[u])){
u=i;
}
}
if(u==-1||u==d)break;
visited[u]=1;
for(int v=0;v<n;v++){
//如果uv连通
if(graph[u][v].length<INT_MAX){
if(dist[v]>dist[u]+graph[u][v].length){
dist[v]=dist[u]+graph[u][v].length;
totalcost[v]=totalcost[u]+graph[u][v].cost;
}
else if(dist[v]==dist[u]+graph[u][v].length){
if(totalcost[v]>totalcost[u]+graph[u][v].cost){
totalcost[v]=totalcost[u]+graph[u][v].cost;
}
}
}
}
}
cout << dist[d] << " " << totalcost[d];
return 0;
}
三、实验使用环境(本次实验所使用的平台和相关软件)
操作系统:Windows 11
编程语言:C++
开发工具:PTA,visual studio 2022
编译器:g++ 17
四、实验步骤和调试过程(实验步骤、测试数据设计、测试结果分析)
五、实验小结(实验中遇到的问题及解决过程、实验体会
实验体会和收获:
掌握了图基本操作
掌握了求最小生成树和最短路径的算法