@Chilling
2017-04-26T20:04:03.000000Z
字数 3859
阅读 885
最短路
Description
Christmas is coming to KCM city. Suby the loyal civilian in KCM city is preparing a big neat Christmas tree. The simple structure of the tree is shown in right picture.
The tree can be represented as a collection of numbered nodes and some edges. The nodes are numbered 1 through n. The root is always numbered 1. Every node in the tree has its weight. The weights can be different from each other. Also the shape of every available edge between two nodes is different, so the unit price of each edge is different. Because of a technical difficulty, price of an edge will be (sum of weights of all descendant nodes) × (unit price of the edge).
Suby wants to minimize the cost of whole tree among all possible choices. Also he wants to use all nodes because he wants a large tree. So he decided to ask you for helping solve this task by find the minimum cost.
Input
The input consists of T test cases. The number of test cases T is given in the first line of the input file. Each test case consists of several lines. Two numbers v, e (0 ≤ v, e ≤ 50000) are given in the first line of each test case. On the next line, v positive integers wi indicating the weights of v nodes are given in one line. On the following e lines, each line contain three positive integers a, b, c indicating the edge which is able to connect two nodes a and b, and unit price c.
All numbers in input are less than 216.
Output
For each test case, output an integer indicating the minimum possible cost for the tree in one line. If there is no way to build a Christmas tree, print “No Answer” in one line.
Sample Input
2
2 1
1 1
1 2 15
7 7
200 10 20 30 40 50 60
1 2 1
2 3 3
2 4 2
3 5 4
3 7 2
3 6 3
1 5 9
Sample Output
15
1210
题意:哈理工1419是本题的中文题意。
分析: 第二个样例
得到:最小的答案=每个点*该点到根结点的最短路
注意: INF开到0x3f3f3f3f3f3f3f3f
#include<queue>
#include<stdio.h>
#include<vector>
#include<string.h>
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;
typedef long long LL;
const int maxn=50005;
int vv[maxn];
int pos[maxn*2],vis[maxn],num;
LL dis[maxn],ans;
struct node1
{
int en,val,next;
}a[maxn*2];
struct node
{
int id,val;
node(int idd=0,int vall=0)
{
id=idd;
val=vall;
}
friend bool operator <(node x,node y)
{
return x.val>y.val;
}
};
void add(int st,int en,int val)
{
a[num].en=en;
a[num].val=val;
a[num].next=pos[st];
pos[st]=num++;
}
void dij()
{
dis[1]=0;
priority_queue<node>q;
q.push(node(1,dis[1]));
while(!q.empty())
{
node now=q.top();
q.pop();
if(vis[now.id]) continue;
vis[now.id]=1;
for(int i=pos[now.id];i!=-1;i=a[i].next)
{
if(!vis[a[i].en]&&dis[a[i].en]>dis[now.id]+a[i].val)
{
dis[a[i].en]=dis[now.id]+a[i].val;
q.push(node(a[i].en,dis[a[i].en]));
}
}
}
}
void init()
{
ans=0;
num=0;
memset(vis,0,sizeof(vis));
memset(dis,0x3f,sizeof(dis));
memset(pos,-1,sizeof(pos));
}
int main()
{
int t;
int v,e;
int x,y,z;
scanf("%d",&t);
while(t--)
{
init();
scanf("%d%d",&v,&e);
for(int i=1;i<=v;i++)
scanf("%d",&vv[i]);
while(e--)
{
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
add(y,x,z);
}
dij();
int flag=1;
for(int i=1;i<=v;i++)
{
if(dis[i]==INF)
{
flag=0;
break;
}
ans+=dis[i]*vv[i];
}
if(flag)
printf("%lld\n",ans);
else
printf("No Answer\n");
}
return 0;
}
#include<stdio.h>
#include<string.h>
#include<vector>
#include<queue>
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;
typedef long long LL;
const int maxn=50005;
int v,e;
int vv[maxn];
int cnt[maxn],vis[maxn];
struct node
{
int en,val,next;
}a[maxn*2];
int pos[maxn],num;
LL dis[maxn],ans;
void add(int st,int en,int val)
{
a[num].en=en;
a[num].val=val;
a[num].next=pos[st];
pos[st]=num++;
}
void init()
{
num=0;
ans=0;
memset(pos,-1,sizeof(pos));
}
void spfa()
{
queue<int>q;
for(int i=1;i<=v;i++)
dis[i]=INF,vis[i]=0,cnt[i]=0;
dis[1]=0;
cnt[1]++;
q.push(1);
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=0;
for(int i=pos[u];i!=-1;i=a[i].next)
{
int w=a[i].en;
int val=a[i].val;
if(dis[w]>dis[u]+val)
{
dis[w]=dis[u]+val;
if(!vis[w])
{
vis[w]=1;
cnt[w]++;
if(cnt[w]>=v)
return;
q.push(w);
}
}
}
}
}
int main()
{
int t;
int x,y,z;
scanf("%d",&t);
while(t--)
{
init();
scanf("%d%d",&v,&e);
for(int i=1;i<=v;i++)
scanf("%d",&vv[i]);
while(e--)
{
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
add(y,x,z);
}
spfa();
int flag=1;
for(int i=1;i<=v;i++)
{
if(dis[i]==INF)
{
flag=0;
break;
}
ans+=dis[i]*vv[i];
}
if(flag)
printf("%lld\n",ans);
else
printf("No Answer\n");
}
return 0;
}