2 条题解

  • 1
    @ 2022-11-13 15:37:25
    /*****************************************
    Note:
    ******************************************/
    #include <queue>
    #include <set>
    #include <math.h>
    #include <stack>
    #include <stdio.h>
    #include <iostream>
    #include <vector>
    #include <iomanip>
    #include <string.h>
    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    #define LL long long
    const int N = 1e6 + 10;
    const int INF = 0x3f3f3f3f;
    int n,m,ans,i,j,k;
    int head[N],ne[N],to[N],dis[N],w[N],id;
    bool vis[N];
    void add(int x,int y,int z)
    {
    	to[++id]=y,w[id]=z,ne[id]=head[x],head[x]=id;
    }
    struct node{
    	int id,num;
    	bool operator<(const node &a)const
    	{
    		return num>a.num;
    	}
    };
    void dij()
    {
    	priority_queue<node>p;
    	p.push((node){1,0});
    	p.push((node){n+1,0});
    	dis[1]=dis[n+1]=0;
    	while(!p.empty())
    	{
    		node t=p.top();
    		p.pop();
    		if(vis[t.id]||dis[t.id]!=t.num)
    			continue;
    		vis[t.id]=1;
    		for(int i=head[t.id];i;i=ne[i])
    		{
    			int v=to[i];
    			if(!vis[v]&&dis[v]>dis[t.id]+w[i])
    			{
    				dis[v]=dis[t.id]+w[i];
    				p.push((node){v,dis[v]});
    			}
    		}
    	 } 
    }
    int main()
    {
    	memset(dis,0x3f,sizeof vis);
    	cin>>n>>m;
    	for(int i=1;i<=m;++i)
    	{
    		int x,y,z;
    		cin>>x>>y>>z;
    		add(x,y,z);
    		add(y+n,x+n,z);
    	}
    	dij();
    	int ans=0;
    	for(int i=1;i<=2*n;++i)
    		ans+=dis[i];
    	cout<<ans;
    	return 0;
    }
    
    • 1
      @ 2022-11-13 15:31:22
      备注:
      ******************************************/
      #include <queue>
      #include <math.h>
      #include <stack>
      #include <stdio.h>
      #include <iostream>
      #include <vector>
      #include <iomanip>
      #include <string.h>
      #include <algorithm>
      #include <map>
      using namespace std;
      #define LL long long
      const int N = 1e6 + 10;
      const int INF = 0x3f3f3f3f;
      int n, m;
      int head[N], ne[N], to[N], w[N], id;
      int dis[N], vis[N];
      void add(int x, int y, int z)
      {
      	to[++id] = y, w[id] = z, ne[id] = head[x], head[x] = id;
      }
      struct node
      {
      	int id, num;
      	bool operator <(const node &a)const
      	{
      		return num > a.num;
      	}
      };
      void dij()
      {
      	priority_queue<node> p;
      	p.push((node){1, 0});
      	p.push((node){n+1, 0});
      	dis[1] = dis[n+1] = 0;
      	while(!p.empty())
      	{
      		node t = p.top();
      		p.pop();
      		if(vis[t.id] || dis[t.id] != t.num)
      			continue;
      		vis[t.id] = 1;
      		for(int i = head[t.id]; i; i = ne[i])
      		{
      			int v = to[i];
      			if(!vis[v] && dis[v] > dis[t.id] + w[i])
      			{
      				dis[v] = dis[t.id] + w[i];
      				p.push((node){v, dis[v]});
      			}
      		}
      	}
      }
      signed main()
      {
      	//freopen(".in", "r", stdin);
      	//freopen(".out", "w", stdout);
      	memset(dis, 0x3f, sizeof dis);
      	cin>>n>>m;
      	for(int i = 1, x, y, z; i <= m; i++)
      	{
      		cin>>x>>y>>z;
      		add(x, y ,z);
      		add(y + n, x + n, z);
      	}
      	dij();
      	int ans = 0;
      	for(int i = 1; i <= 2*n; i++)
      	{
      		ans += dis[i];
      	}
      	cout<<ans<<endl;
      	return 0;
      }
      
      
      • 1

      信息

      ID
      410
      时间
      1000ms
      内存
      256MiB
      难度
      6
      标签
      递交数
      74
      已通过
      24
      上传者