pat emergency

在这里插入代码片
```#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而不是零,我一脸蒙蔽


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值