鬼畜的操作增加了
查看原帖
鬼畜的操作增加了
157598
Magallan_forever楼主2020/8/10 20:23

我建两遍图并且跑两边最短路以后TLE80,然后我发现我cnt没清零(链星),所以为什么cnt没清零不会WARE

#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;
const int maxp=1e3+1,maxe=1e5+1;
int n,dist[maxp];
bool mark[maxp];
int last_[maxp],next_[maxe],to_[maxe],cost[maxe],cnt=0;
void link(int x,int y,int c) { next_[cnt]=last_[x],to_[cnt]=y,cost[cnt]=c,last_[x]=cnt++; }
struct node{
	int now,dist;
	node(int p=0,int d=0) :now(p),dist(d) {}
	bool operator<(const node& a) const { return dist>a.dist; }
};
priority_queue<node> pq;
void dijkstra(int s){
	memset(dist,0x3f,sizeof(dist)),memset(mark,false,sizeof(mark));
	while(!pq.empty()) pq.pop();
	node temp(s);
	pq.push(temp),dist[s]=0;
	while(!pq.empty()){
		temp=pq.top(),pq.pop();
		if(mark[temp.now]) continue;
		for(int i=last_[temp.now];~i;i=next_[i]) if(cost[i]+dist[temp.now]<dist[to_[i]]) dist[to_[i]]=cost[i]+dist[temp.now],pq.push(node(to_[i],cost[i]+dist[temp.now]));
	}
}
int x[maxe],y[maxe],c[maxe];
int main(){
	int n,m,ans=0;
	scanf("%d%d",&n,&m),memset(last_,-1,sizeof(last_));
	for(int i=0;i<m;++i) scanf("%d%d%d",x+i,y+i,c+i),link(x[i],y[i],c[i]);
	dijkstra(1);
	for(int i=2;i<=n;++i) ans+=dist[i];
	memset(last_,-1,sizeof(last_)),cnt=0;
	for(int i=0;i<m;++i) link(y[i],x[i],c[i]);
	dijkstra(1);
	for(int i=2;i<=n;++i) ans+=dist[i];
	printf("%d",ans);
	return 0;
}
2020/8/10 20:23
加载中...