样例已过WA求调谢谢
查看原帖
样例已过WA求调谢谢
405111
HeHNCu楼主2024/10/19 10:48
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<vector>
#include<queue>
#define maxn 505
using namespace std;
struct Edge{
	int to,val;
	Edge(){}
	Edge(int t,int v){
		to = t;
		val = v;
	}
};
vector<Edge>G[maxn];
void add(int,int,int);
int n,s,e,m,k; 
struct node{
	int u,d;
	bool operator < (const node & rhs) const{return d < rhs.d;} 
	node(){}
	node(int u,int d){
		this->u = u;
		this->d = d;
	} 
};
bool vis[maxn];
int dis[maxn],pre1[maxn],pre2[maxn];
int f[maxn]/*存放第一遍的dijkstra的距离*/,g[maxn]/*第二遍*/;
void dijkstra(int,int*);
void printCounterClockWise(int now){
	if(now == s){
		cout<<now;
		return;
	}
	int nxt = pre1[now];
	printCounterClockWise(nxt);
	cout<<" "<<now;
}
void printClockWise(int now){
	if(now == e){
		cout<<" "<<now;
		return;
	}
	int nxt = pre2[now];
	cout<<" "<<now;
	printClockWise(nxt);
	
}
int main(){
	while(cin>>n>>s>>e){
		for(int i=1;i<=n;i++)G[i].clear();
		int x,y,z;
		cin>>m;
		for(int i=1;i<=m;i++){
			cin>>x>>y>>z;
			add(x,y,z);
		}
		dijkstra(s,pre1);
		memcpy(f,dis,sizeof dis); 
		dijkstra(e,pre2);
		memcpy(g,dis,sizeof dis);
		cin>>k;
		int ans=f[e];
		/*
		* 当前时间段的最大值,如果必须使用商务票也是可以
		* 在后面在枚举商务票的时候也是可以算进来的 
		*/ 
        int st=-1,ed=-1;// 使用票的开始和结束 
		while(k--){
			cin>>x>>y>>z;
			if(ans > f[x] + g[y] + z){//从x到y 
				ans = f[x] + g[y] + z;
				st = x;
				ed = y;
			}
			if(ans > f[y] + g[x] + z){
                ans = f[y] + g[x] + z;
                st = y;
                ed = x;
            }
		}
		if(st == -1){
			printCounterClockWise(e);
			cout<<"\nTicket Not Used\n";
		}else{
			printCounterClockWise(st);
            printClockWise(ed);
            printf("\n%d\n", st);
		}
		printf("%d\n", ans);
	}
}
void add(int x,int y,int z){
	G[x].push_back(Edge(y,z));
	G[y].push_back(Edge(x,z));
}
void dijkstra(int o,int* pre){
	priority_queue<node>pn;
	pn.push(node(o,0));
	memset(vis,0,sizeof vis);
	memset(dis,0x3f,sizeof dis);
	dis[o]=0;
	while(pn.size()){
		int u = pn.top().u,d = pn.top().d;
		pn.pop();
		if(vis[u])continue;
		vis[u]=true;
		for(Edge nxt : G[u]){
			int v = nxt.to,val = nxt.val;
			if(dis[v] > dis[u] + val){
				dis[v] = dis[u] + val;
				pre[v] = u;
				pn.push(node(v,dis[v])); 
			} 
		}
	}
}
2024/10/19 10:48
加载中...