2017 ACM-ICPC Asia Regional Shenyang Online spfa+最长路

transaction transaction transaction

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)
Total Submission(s): 1496    Accepted Submission(s): 723


Problem Description
Kelukin is a businessman. Every day, he travels around cities to do some business. On August 17th, in memory of a great man, citizens will read a book named "the Man Who Changed China". Of course, Kelukin wouldn't miss this chance to make money, but he doesn't have this book. So he has to choose two city to buy and sell. 
As we know, the price of this book was different in each city. It is  ai yuan in it city. Kelukin will take taxi, whose price is 1yuan per km and this fare cannot be ignored.
There are n1 roads connecting n cities. Kelukin can choose any city to start his travel. He want to know the maximum money he can get.
 

 

Input
The first line contains an integer  T (1T10) , the number of test cases. 
For each test case:
first line contains an integer n (2n100000) means the number of cities;
second line contains n numbers, the ith number means the prices in ith city; (1Price10000) 
then follows n1 lines, each contains three numbers xy and z which means there exists a road between x and y, the distance is zkm (1z1000)
 

 

Output
For each test case, output a single number in a line: the maximum money he can get.
 

 

Sample Input
1 4 10 40 15 30 1 2 30 1 3 2 3 4 10
 

 

Sample Output
8
 

 

Source
2017 ACM/ICPC Asia Regional Shenyang Online
 
思路:建立一个原点0和汇点n+1,将所有点从0到n+1的可能路径长度取最大值输出即可。
代码:
 1 #include
 2 //#include
 3 #define db double
 4 #define ll long long
 5 #define vec vector
 6 #define Mt  vector
 7 #define ci(x) scanf("%d",&x)
 8 #define cd(x) scanf("%lf",&x)
 9 #define cl(x) scanf("%lld",&x)
10 #define pi(x) printf("%d\n",x)
11 #define pd(x) printf("%f\n",x)
12 #define pl(x) printf("%lld\n",x)
13 #define MP make_pair
14 #define PB push_back
15 #define fr(i,a,b) for(int i=a;i<=b;i++)
16 using namespace std;
17 const int N=1e6+5;
18 const int mod=1e9+7;
19 const int MOD=mod-1;
20 const db  eps=1e-18;
21 const db  pi = acos(-1.0);
22 const int inf = 0x3f3f3f3f;
23 const ll  INF = 0x3f3f3f3f3f3f3f3f;
24 int a[N],d[N],vis[N];
25 
26 struct P
27 {
28     int u,v,w;
29     P(int x,int y,int z):u(x),v(y),w(z){};
30     P(){};
31 };
32 vector

g[N],e; 33 queue<int> q; 34 void add(int x,int y,int z) 35 { 36 g[x].push_back(P(x,y,z)); 37 } 38 void spfa(int n) 39 { 40 memset(d,0, sizeof(d)); 41 memset(vis,0, sizeof(vis)); 42 vis[0]=1; 43 q.push(0); 44 while(q.size()) 45 { 46 int u=q.front();q.pop(); 47 vis[u]=0; 48 for(int i=0;i){ 49 int v=g[u][i].v; 50 int w=g[u][i].w; 51 if(d[v]// get the maxmum 52 d[v]=d[u]+w; 53 if(!vis[v]){ 54 vis[v]=1;//push the new point 55 q.push(v); 56 } 57 } 58 } 59 } 60 pi(d[n+1]); 61 } 62 int main() 63 { 64 int t; 65 ci(t); 66 while(t--) 67 { 68 int n; 69 ci(n); 70 for(int i=0;i<=n;i++) g[i].clear(); 71 for(int i=1;i<=n;i++) 72 ci(a[i]),add(0,i,a[i]),add(i,n+1,-a[i]); 73 for(int i=1;i){ 74 int x,y,z; 75 ci(x),ci(y),ci(z); 76 add(x,y,-z); 77 add(y,x,-z); 78 } 79 spfa(n); 80 81 } 82 83 }

 

你可能感兴趣的:(2017 ACM-ICPC Asia Regional Shenyang Online spfa+最长路)