在这里插入代码片
```#include<cstdio>
#include<iostream>
#include<cstdlib>
using namespace std;
typedef struct graph* Graph;
struct graph{
int vn , en;
struct headtoptr* G;
};
typedef struct headtoptr* Headtoptr;
struct headtoptr{
int v;
int teamnum;
struct nodetoptr* next;
};
typedef struct nodetoptr* Nodetoptr;
struct nodetoptr{
int v;
int length;
struct nodetoptr* next;
};
typedef struct edge Edge;
struct edge{
int v1 , v2;
int length;
};
int start , endc;
int totalnum = 0 ,maxteam = 0 , minlength = 9999999 ,currentlength = 0,currentteam=0;
bool isp[510] = {false};
Graph CreateGraph(int n){
Graph newG = (Graph)malloc(sizeof(struct graph));
newG->vn = n;
newG->en = 0;
newG->G = (Headtoptr)malloc(n*sizeof(struct headtoptr));
for(int i = 0 ; i < n ; i++){
newG->G[i].v = i;
newG->G[i].next = NULL;
}
return newG;
}
void Ginsert(Graph graph , Edge edge){
int v2 = edge.v2;
int length = edge.length;
Nodetoptr node = (Nodetoptr)malloc(sizeof(struct nodetoptr));
node->v = v2;
node->length = length;
node->next = graph->G[edge.v1].next;
graph->G[edge.v1].next = node;
int v1 = edge.v1;
Nodetoptr node1 = (Nodetoptr)malloc(sizeof(struct nodetoptr));
node1->v = v1;
node1->length = length;
node1->next = graph->G[edge.v2].next;
graph->G[edge.v2].next = node1;
graph->en++;
}
void BFS(Graph graph,Nodetoptr node){
isp[node->v] = true;
currentlength+=node->length;
currentteam+=graph->G[node->v].teamnum;
if(node->v == endc){
if(currentlength == minlength){
totalnum++;
if(currentteam > maxteam){
maxteam = currentteam;
}
}else if(currentlength < minlength){
totalnum = 1;
maxteam = currentteam;
minlength = currentlength;
}
}else{
Nodetoptr w = graph->G[node->v].next;
while(w){
if(!isp[w->v])
BFS(graph , w);
w = w->next;
}
}
isp[node->v] = false;
currentlength-=node->length;
currentteam-=graph->G[node->v].teamnum;
}
int main(){
int nodenum , edgenum;
cin>>nodenum>>edgenum>>start>>endc;
Graph graph = CreateGraph(nodenum);
for(int i = 0 ; i < nodenum ; i++){
scanf("%d",&graph->G[i].teamnum);
}
while(edgenum--){
int sc , ec , le;
cin>>sc>>ec>>le;
Edge edge;
edge.v1 = sc;
edge.v2 = ec;
edge.length = le;
Ginsert(graph , edge);
}
if(start != endc){
isp[start] = true;
currentteam+=graph->G[start].teamnum;
Nodetoptr w = graph->G[start].next;
while(w){
BFS(graph,w);
w = w->next;
}
printf("%d %d",totalnum,maxteam);
}else{
printf("%d %d",1,graph->G[start].teamnum);
}
}
//我吐了,最后如果起点等于终点,路径的个数应该是1而不是零,我一脸蒙蔽
04-19
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交