# poj1511小灶第三次作业（dijstra）

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=109960#problem/F

#include <cstring>
#include <cstdio>
#include <queue>
#include <iostream>
#include <vector>
#include <deque>
#include <utility>
#include <functional>
const int maxnum = 1000;
#define maxn 10000

using namespace std;
int dis[maxn][maxn];
int book[maxnum] ,n;
typedef pair <int ,int> pii;
priority_queue <pii,vector<pii> ,greater <pii> > pq;

void ppqq(int lon1[])
{
int node ,flag = 0 ,lon ;
while (!pq.empty())
{
lon = pq.top().first;
node = pq.top().second;

pq.pop();

if(book[node] == 1)
continue;
lon1[node] = lon;
book[node] = 1;
flag++;

if(flag == n)
break;
for (int i = 1 ; i <= n ;i++)
{
if( !book[i] && dis[node][i])
pq.push(make_pair(lon+dis[node][i] , i));
}
}
return ;
}

void ppq(int lon2[])
{
int node ,flag = 0 ,lon ;
while (!pq.empty())
{
lon = pq.top().first;
node = pq.top().second;

pq.pop();

if(book[node] == 1)
continue;

lon2[node] = lon;
book[node] = 1;
flag++;

if(flag == n)
break;
for (int i = 1 ; i <= n ;i++)
{
if( !book[i] && dis[i][node])
pq.push(make_pair(lon+dis[i][node] , i));
}
}

return ;
}

int main()
{
int T;
int lon1[maxnum],lon2[maxnum] ;
scanf("%d",&T);

while (T--)
{
int m,u,v,w;
scanf("%d%d",&n,&m);
memset(dis,0,sizeof(dis));
memset(lon1,0,sizeof(lon1));
memset(lon2,0,sizeof(lon2));
memset(book,0,sizeof(book));

for (int i = 0 ; i < m ;i++)
{
scanf("%d%d%d",&u,&v,&w);
dis[u][v] = w;
}

pq.push(make_pair(0,1));
ppqq(lon1);

while (!pq.empty())
pq.pop();
memset(book,0,sizeof(book));

pq.push(make_pair(0,1));
ppq(lon2);

int ans = 0;
for (int i = 1 ; i<= n ;i++)
{
ans += lon1[i] + lon2[i];
printf("%d   %d\n",lon1[i],lon2[i]);
}

printf("%d\n",ans);
}
return 0;
}

#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
#include <cstring>
const int maxnum = 1000010;

using namespace std;

struct node
{
int u,v,w;
bool operator < (const node &a) const
{
return w > a.w;
}

};

priority_queue <node> pq;
int ans = 0, n,m,book[maxnum];
struct node edg[maxnum];
int s[maxnum];

void dj1(struct node edg[])
{
int node ,flag = 0 ,lon ;
while (!pq.empty())
{
lon = pq.top().w;
node = pq.top().v;

pq.pop();

if(book[node] == 1)
continue;
ans += lon;
//  printf("dj1 %d\n",lon);
book[node] = 1;
flag++;

if(flag == n)
break;
for (int i = 1 ; i <= m ;i++)
{
// printf("qishidian%d   %d\n",edg[i].u,edg[i].v);
if( !book[edg[i].v] && edg[i].u == node)
{
// printf("qishidian%d   %d\n",edg[i].u,edg[i].v);
edg[i].w += lon ;
s[i] += lon;
pq.push(edg[i]);
}
}
}
return ;
}

void dj2(struct node edg[])
{
int node ,flag = 0 ,lon ;
while (!pq.empty())
{
lon = pq.top().w;
node = pq.top().u;

pq.pop();

if(book[node] == 1)
continue;

ans += lon;
//printf("dj2 nod = %d  lon =   %d\n",node ,lon);
book[node] = 1;
flag++;

if(flag == n)
break;
for (int i = 1 ; i <= m ;i++)
{
//printf("一定要找到你%d   %d    %d\n",edg[i].u,edg[i].v,edg[i].w);
if( !book[edg[i].u]  && edg[i].v == node)
{
//    printf("目前的%d   %d    %d\n",edg[i].u,edg[i].v,edg[i].w);
edg[i].w += lon;
//  printf("改变后%d   %d    %d\n",edg[i].u,edg[i].v,edg[i].w);
pq.push(edg[i]);
}
}
}

return ;
}

int main()
{
int t;
scanf("%d",&t);

while (t--)
{
ans = 0;

memset(s,0,sizeof(s));
memset(book,0,sizeof(book));
scanf("%d%d",&n,&m);

for (int i = 1 ; i <= m ;i++)
scanf("%d%d%d",&edg[i].u,&edg[i].v,&edg[i].w);
edg[0].u = 1;
edg[0].v = 1;
edg[0].w = 0;

pq.push(edg[0]);
dj1(edg);

while (!pq.empty())
pq.pop();
memset(book,0,sizeof(book));
for (int i = 1 ; i <= m ;i++)
edg[i].w -= s[i];

pq.push(edg[0]);
dj2(edg);

printf("%d\n",ans);
}
}

#include <climits>
#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <functional>
#include <utility>
#define maxnum 1000010
using namespace std;

struct edg
{
int to;
int dis;
};

typedef pair <int ,int > pii;
vector <edg> from[maxnum];
int d[maxnum];
int u[maxnum],v[maxnum],w[maxnum];
priority_queue < pii , vector <pii> ,greater <pii> > pq;

void dij1()
{
while ( !pq.empty())
{

int ss = pq.top().first;
int node = pq.top().second;
pq.pop();

if(d[node] < ss )
continue;
for (int i = 0 ; i < from[node].size();i++)
{
edg e = from[node][i];
if(d[e.to] > d[node] + e.dis)
{
d[e.to] = d[node] + e.dis;
pq.push(make_pair(d[e.to],e.to));
}
}

}
}

int main()
{
int t;
scanf("%d",&t);

while (t--)
{
int n , m ;
scanf("%d%d",&n,&m);
long long int ans = 0;
for (int i = 0 ; i < m ;i++)
scanf("%d%d%d",&u[i],&v[i],&w[i]);

for (int i = 0 ; i < m ;i++)
{
edg e;
e.to=v[i];
e.dis=w[i];
from[u[i]].push_back(e);
}
for (int i = 1 ; i <= n ;i++)
d[i] = INT_MAX;

pq.push(make_pair(0,1));
d[1] = 0;
dij1();
for (int i = 1 ;i <= n ;i++)
ans += d[i];

for (int i = 0 ;i < m ;i++)
from[i].clear();
for (int i = 1 ; i <= n ;i++)
d[i] = INT_MAX;
for (int i = 0 ; i < m ;i++)
{
edg e;
e.to=u[i];
e.dis=w[i];
from[v[i]].push_back(e);
}
pq.push(make_pair(0,1));
d[1] = 0;
dij1();
for (int i = 1 ;i <= n ;i++)
ans += d[i];

printf("%I64d\n",ans);
for (int i = 0 ;i < m ;i++)
from[i].clear();
}
return 0;
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120