2 条题解

  • 0
    #include<bits/stdc++.h>
    #define PII pair<int,int>
    using namespace std;
    
    const int N=1e5+10;
    int dis[N],hd[N],n,m,s,t,idx=1;
    struct Glist{
    	int to,w,nxt;
    }e[N];
    bool vis[N];
    
    void add(int a,int b,int c)
    {
    	e[idx].w=c;
    	e[idx].to=b;
    	e[idx].nxt=hd[a];
    	hd[a]=idx++; 
    }
    
    int Dijkstra()
    {
    	memset(dis,0x3f,sizeof(dis));
    	dis[s]=0;
    	priority_queue< PII,vector<PII>,greater<PII> >q;
    	q.push( {0,s} );
    	while(q.size())
    	{
    		PII t=q.top();
    		int ver=t.second,dist=t.first;
    		q.pop();
    		
    		if(vis[ver])continue;
    		vis[ver]=1;
    		
    		for(int i=hd[ver];i!=-1;i=e[i].nxt)
    		{
    			int j=e[i].to;
    			if(dis[j]>dis[ver]+e[i].w)
    			{
    				dis[j]=dis[ver]+e[i].w;
    				q.push( {dis[j],j} );
    			}
    		}
    	}
    	if(dis[t]==0x3f3f3f3f)
    	{
    		cout<<"No Solution!";
    		return -1;
    	}
    	return dis[t]; 
    }
    
    int main()
    {
    	cin>>n>>m>>s>>t;
    	memset(hd,-1,sizeof(hd));
    	for(int i=1;i<=m;++i)
    	{
    		int a,b,c;
    		cin>>a>>b>>c;
    		add(a,b,c);
    		add(b,a,c);
    	}
    	cout<<Dijkstra();
    	return 0;
    } 
    
    • 0
      @ 2023-7-23 14:41:17

      把模板改一下就好了

      #include<bits/stdc++.h>
      
      const int MaxN = 100010, MaxM = 500010;
      
      struct edge
      {
          int to, dis, next;
      };
      
      edge e[MaxM];
      int head[MaxN], dis[MaxN], cnt;
      bool vis[MaxN];
      int n, m, s, endd;
      
      inline void add_edge( int u, int v, int d )
      {
          cnt++;
          e[cnt].dis = d;
          e[cnt].to = v;
          e[cnt].next = head[u];
          head[u] = cnt;
      }
      
      struct node
      {
          int dis;
          int pos;
          bool operator <( const node &x )const
          {
              return x.dis < dis;
          }
      };
      
      std::priority_queue<node> q;
      
      
      inline void dijkstra()
      {
          dis[s] = 0;
          q.push( ( node ){0, s} );
          while( !q.empty() )
          {
              node tmp = q.top();
              q.pop();
              int x = tmp.pos, d = tmp.dis;
              if( vis[x] )
                  continue;
              vis[x] = 1;
              for( int i = head[x]; i; i = e[i].next )
              {
                  int y = e[i].to;
                  if( dis[y] > dis[x] + e[i].dis )
                  {
                      dis[y] = dis[x] + e[i].dis;
                      if( !vis[y] )
                      {
                          q.push( ( node ){dis[y], y} );
                      }
                  }
              }
          }
      }
      
      
      int main()
      {
          scanf( "%d%d%d%d", &n, &m, &s ,&endd );
          for(int i = 1; i <= n; ++i)dis[i] = 0x7fffffff;
          for( register int i = 0; i < m; ++i )
          {
              register int u, v, d;
              scanf( "%d%d%d", &u, &v, &d );
              add_edge( u, v, d );add_edge( v, u, d );
          }
          dijkstra();
          printf( "%d ", dis[endd] );
          return 0;
      }
      
      
      • 1

      信息

      ID
      1316
      时间
      1000ms
      内存
      512MiB
      难度
      7
      标签
      递交数
      102
      已通过
      20
      上传者